完整注册登陆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">php好看主页源码
function InputCheck(LoginForm){
if(LoginForm.username.value==''){
alert('请输⼊⽤户名!');
LoginForm.username.focus();
return (false);
}
if(LoginForm.password.value==''){
alert('请输⼊密码!');
LoginForm.password.focus();
return (false);
}
ail.value==''){
alert('请输⼊邮箱');
return (false);
}
}
</script>
<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>');
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);
}
?>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论