完整注册登陆php源码,附带session验证。
1、首先先写表单页面login.html.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户登陆</title>
</head>
<!-- 定义输入表单样式 -->
<style type="text/css">
html{font-size: 12px;}
fieldset{width:200px;margin:0 auto;}
legend{font-weight: bold; font-size: 14px;}
label{float:left; width:70px; margin-left:10px;}
.left{margin-left:80px;}
.input{width:150px;}
span{color: #666666;}
</style>
<!-- 验证表单是否输入 -->
<script type="text/javascript">
function InputCheck(LoginForm){
if(LoginForm.username.value==''){
alert('请输入用户名!');
LoginForm.username.focus();
return (false);
}
if(LoginForm.password.value==''){
alert('请输入密码!');
LoginForm.password.focus();
return (false);
}
}
</script>
<body>
<div>
<fieldset>
<legend>用户登陆</legend>
<form name="LoginForm" method="post" action="login.php" onSubmit="return InputCheck(this)">
<p>
<lable for="username" class="lable">用户名:</lable>
<input id="username" name="username" type="text" class="input" />
</p>
<p>
<lable for="password" class="lable">密 码:</lable>
<input id="password" name="password" type="text" class="input" />
</p>
<p>
<input name="submit" value="提交" type="submit" />
<a href="reg.html">点此注册</a>
</p>
</form>
</fieldset>
</div>
</body>
</html>
2、然后写登陆源码。
<?php
session_start();
//注销登录
function logout(){
if($_GET['action']='logout'){
unset($_SESSION['user_id']);
unset($_SESSION['username']);
echo '注销登陆成功!点击此处<a href="javascript:history.back(-1);">登陆</a>';
exit;
}
}
//登陆
if(!isset($_POST['submit'])){
exit('非法访问!');
}
$username = htmlspecialchars($_POST['username']);
$password = md5($_POST['password']);
//引入数据库文件
include('mysql.php');
//检测用户名密码是否正确
$where = 'username="'.$username.'"';
if($rs=mysql_fetch_assoc(select('user',$where))){
//登陆成功
$_SESSION['name'] = $username;
$_SESSION['userid'] = $rs['user_id'];
echo $username,' 欢迎你!进入 <a href="my.php">用户中心</a><br />';
echo '点击此处 <a href="login.php?action=logout">注销</a> 登录!<br />';
exit;
}else {
exit('登录失败!点击此处 <a href="javascript:history.back(-1);">返回</a> 重试');
}
3、注册页面表单
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户登陆</title>
</head>
<!-- 定义输入表单样式 -->
<style type="text/css">
html{font-size: 12px;}
fieldset{width:200px;margin:0 auto;}
legend{font-weight: bold; font-size: 14px;}
label{float:left; width:70px; margin-left:10px;}
.left{margin-left:80px;}
.input{width:150px;}
span{color: #666666;}
</style>
<!-- 验证表单是否输入 -->
<script type="text/javascript">
function InputCheck(LoginForm){
if(LoginForm.username.value==''){
alert('请输入用户名!');
LoginForm.username.focus();
return (false);
}
if(LoginForm.password.value==''){
alert('请输入密码!');
LoginForm.password.focus();
return (false);
}
if(LoginForm.email.value==''){
alert('请输入邮箱');
LoginForm.email.focus();
return (false);
}
}
</script>
<body>
<div>
<fieldset>
<legend>用户登陆</legend>
<form name="LoginForm" method="post" action="reg.php" onSubmit="return InputCheck(this)">
<p>
<lable for="username" class="lable">用户名:</lable>
<input id="username" name="username" type="text" class="input" />
</p>
<p>
<lable for="password" class="lable">密 码:</lable>
<input id="password" name="password" type="text" class="input" />
</p>
<p>
<lable for="password" class="lable">邮 箱:</lable>
<input id="email" name="email" type="text" class="input" />
</p>
<p>
<input name="submit" value="注册" type="submit" />
</p>
</form>
</fieldset>
</div>
</body>
</html>
4、接收注册表单。
<?php
if(!isset($_POST['submit'])){
exit('非法操作!');
}
$username = strtolower($_POST['username']);
$password = strtolower($_POST['password']);
$email = strtolower($_POST['email']);
//注册信息判断
// if(!preg_match('/^[\w\x80-\xff]{3-15}$/',$username)){
// exit('错误:用户名不符合规定。<a href="javascript:history.back(-1);">返回</a>');
// }
if(strlen($password)<6){
exit('错误:密码长度太短。<a href="javascript:history.back(-1);">返回</a>');
}
// if(!preg_match('/^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/',$email)){
// exit('错误:电子邮件格式有误。<a href="javascript:history.back(-1);">返回</a>');
// }
//引入数据库操作文件
include('mysql.php');
//检测用户名是否存在
$where = 'username="'.$username.'"';
if(mysql_fetch_assoc(select('user',$where))){
echo '错误:用户名',$usename,'已存在.<a href="javascript:history.back(-1);">返回</a>';
exit();
}
//插入数据
$password = md5($password);
$data['username'] = $username;
$data['password'] = $password;
$data['email'] = $_POST['email'];
$re = insert('user',$data);
if($re){
exit('用户注册成功!点此登录<a href=" login.html">登陆</a>');
}else{
echo ' 添加数据失败:',mysql_error(),'<br>';
echo '点击此处<a href="javascript:history.back(-1);">返回</a>重试';
}
5、验证页面是否已经登录
<?php
session_start();
//检测是否登录,若没登录则转向登录界面
if(!isset($_SESSION['userid'])){
header("Location:login.html");
exit();
}
//包含数据库连接文件
include('conn.php');
$userid = $_SESSION['userid'];
$username = $_SESSION['username'];
$user_query = mysql_query("select * from user where uid=$userid limit 1");
$row = mysql_fetch_array($user_query);
echo '用户信息:<br />';
echo '用户ID:',$userid,'<br />';
echo '用户名:',$username,'<br />';
echo '邮箱:',$row['email'],'<br />';
echo '注册日期:',date("Y-m-d", $row['regdate']),'<br />';
echo '<a href="login.php?action=logout">注销</a> 登录<br />';
6、操作数据库函数。
<?php
$db_host = "localhost:3306";
// database name
$db_name = "test";
// database username
$db_user = "root";
// database password
$db_pass = "";
$con = mysql_connect($db_host,$db_user,$db_pass);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_name, $con);
// some code
#mysql_close($con);
function select($table,$where='',$order='',$limits='',$field=''){
$field=($field==null)?'*':$field;
$sele.="from ".$table;
$where=($where==null)?'':' where '.$where;
$order=($order==null)?'':' order by '.$order;
$limits=($limits==null)?'':' limit '.$limits;
$sele="select ".$field." from ".$table.$where.$order.$limits;
//var_dump($sele);
return mysql_query($sele);
mysql_close($con);
}
//插入,二个参数都是必须,后为数组
function insert($table,$columns)
{
foreach($columns as $key=>$values)
{
$addkey.='`'.$key.'`,';
if(is_numeric($values))
{
$addvalues.=$values.',';
}
else
{
$values=mysql_real_escape_string($values);
$addvalues.='\''.$values.'\',';
}
}
$addkey=rtrim($addkey,',');
$addvalues=rtrim($addvalues,',');
$inse.='INSERT INTO '.$table.'('.$addkey.') VALUES ('.$addvalues.');';
return mysql_query($inse);
}
//更新,三个参数都是必须的
function update($table,$data,$where)
{
foreach($data as $k=>$v)
{
if(is_numeric($v))
{
$edit_data.='`'.$k.'`'.'='.$v.',';
}
else
{
$edit_data.='`'.$k.'`'.'='."'".mysql_real_escape_string($v)."',";
}
}
$edit_data=rtrim($edit_data,',');
$upda="UPDATE ".$table." SET ".$edit_data." WHERE ".$where;
return mysql_query($upda);
}
//删除
function delete($table,$where)
{ $dele="DELETE FROM ".$table." WHERE ".$where;
return mysql_query($dele);
}
?>