PHP用户注册与登录完整代码【4】

login.html

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 3 <html xmlns="http://www.w3.org/1999/xhtml">
 4 <head>
 5 <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
 6 <title>用户登录</title>
 7 <style type="text/css">
 8     html{font-size:12px;}
 9     fieldset{width:520px; margin: 0 auto;}
10     legend{font-weight:bold; font-size:14px;}
11     label{float:left; width:70px; margin-left:10px;}
12     .left{margin-left:80px;}
13     .input{width:150px;}
14     span{color: #666666;}
15 </style>
16 <script language=JavaScript>
17 <!--
18 
19 function InputCheck(LoginForm)
20 {
21   if (LoginForm.username.value == "")
22   {
23     alert("请输入用户名!");
24     LoginForm.username.focus();
25     return (false);
26   }
27   if (LoginForm.password.value == "")
28   {
29     alert("请输入密码!");
30     LoginForm.password.focus();
31     return (false);
32   }
33 }
34 
35 //-->
36 </script>
37 </head>
38 <body>
39 <div>
40 <fieldset>
41 <legend>用户登录</legend>
42 <form name="LoginForm" method="post" action="login.php" onSubmit="return InputCheck(this)">
43 <p>
44 <label for="username" class="label">用户名:</label>
45 <input id="username" name="username" type="text" class="input" />
46 <p/>
47 <p>
48 <label for="password" class="label">密 码:</label>
49 <input id="password" name="password" type="password" class="input" />
50 <p/>
51 <p>
52 <input type="submit" name="submit" value="  确 定  " class="left" />
53 </p>
54 </form>
55 </fieldset>
56 </div>
57 </body>
58 </html>

conn.php

 1 <?php
 2 /*****************************
 3 *数据库连接
 4 *****************************/
 5 $conn = @mysql_connect("localhost","root","root123");
 6 if (!$conn){
 7     die("连接数据库失败:" . mysql_error());
 8 }
 9 mysql_select_db("test", $conn);
10 //字符转换,读库
11 mysql_query("set character set 'gbk'");
12 //写库
13 mysql_query("set names 'gbk'");
14 ?>

reg.php

 1 <?php
 2 if(!isset($_POST['submit'])){
 3     exit('非法访问!');
 4 }
 5 $username = $_POST['username'];
 6 $password = $_POST['password'];
 7 $email = $_POST['email'];
 8 //注册信息判断
 9 if(!preg_match('/^[\w\x80-\xff]{3,15}$/', $username)){
10     exit('错误:用户名不符合规定。<a href="javascript:history.back(-1);">返回</a>');
11 }
12 if(strlen($password) < 6){
13     exit('错误:密码长度不符合规定。<a href="javascript:history.back(-1);">返回</a>');
14 }
15 if(!preg_match('/^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/', $email)){
16     exit('错误:电子邮箱格式错误。<a href="javascript:history.back(-1);">返回</a>');
17 }
18 //包含数据库连接文件
19 include('conn.php');
20 //检测用户名是否已经存在
21 $check_query = mysql_query("select uid from user where username='$username' limit 1");
22 if(mysql_fetch_array($check_query)){
23     echo '错误:用户名 ',$username,' 已存在。<a href="javascript:history.back(-1);">返回</a>';
24     exit;
25 }
26 //写入数据
27 $password = MD5($password);
28 $regdate = time();
29 $sql = "INSERT INTO user(username,password,email,regdate)VALUES('$username','$password','$email',
30 $regdate)";
31 if(mysql_query($sql,$conn)){
32     exit('用户注册成功!点击此处 <a href="login.html">登录</a>');
33 } else {
34     echo '抱歉!添加数据失败:',mysql_error(),'<br />';
35     echo '点击此处 <a href="javascript:history.back(-1);">返回</a> 重试';
36 }
37 ?>

login.html

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 3 <html xmlns="http://www.w3.org/1999/xhtml">
 4 <head>
 5 <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
 6 <title>用户登录</title>
 7 <style type="text/css">
 8     html{font-size:12px;}
 9     fieldset{width:300px; margin: 0 auto;}
10     legend{font-weight:bold; font-size:14px;}
11     .label{float:left; width:70px; margin-left:10px;}
12     .left{margin-left:80px;}
13     .input{width:150px;}
14     span{color: #666666;}
15 </style>
16 <script language=JavaScript>
17 <!--
18 
19 function InputCheck(LoginForm)
20 {
21   if (LoginForm.username.value == "")
22   {
23     alert("请输入用户名!");
24     LoginForm.username.focus();
25     return (false);
26   }
27   if (LoginForm.password.value == "")
28   {
29     alert("请输入密码!");
30     LoginForm.password.focus();
31     return (false);
32   }
33 }
34 
35 //-->
36 </script>
37 </head>
38 <body>
39 <div>
40 <fieldset>
41 <legend>用户登录</legend>
42 <form name="LoginForm" method="post" action="login.php" onSubmit="return InputCheck(this)">
43 <p>
44 <label for="username" class="label">用户名:</label>
45 <input id="username" name="username" type="text" class="input" />
46 <p/>
47 <p>
48 <label for="password" class="label">密 码:</label>
49 <input id="password" name="password" type="password" class="input" />
50 <p/>
51 <p>
52 <input type="submit" name="submit" value="  确 定  " class="left" />
53 </p>
54 </form>
55 </fieldset>
56 </div>
57 </body>
58 </html>

login.php

 1 <?php
 2 session_start();
 3 
 4 //注销登录
 5 if($_GET['action'] == "logout"){
 6     unset($_SESSION['userid']);
 7     unset($_SESSION['username']);
 8     echo '注销登录成功!点击此处 <a href="login.html">登录</a>';
 9     exit;
10 }
11 
12 //登录
13 if(!isset($_POST['submit'])){
14     exit('非法访问!');
15 }
16 $username = htmlspecialchars($_POST['username']);
17 $password = MD5($_POST['password']);
18 
19 //包含数据库连接文件
20 include('conn.php');
21 //检测用户名及密码是否正确
22 $check_query = mysql_query("select uid from user where username='$username' and password='$password' 
23 limit 1");
24 if($result = mysql_fetch_array($check_query)){
25     //登录成功
26     $_SESSION['username'] = $username;
27     $_SESSION['userid'] = $result['uid'];
28     echo $username,' 欢迎你!进入 <a href="my.php">用户中心</a><br />';
29     echo '点击此处 <a href="login.php?action=logout">注销</a> 登录!<br />';
30     exit;
31 } else {
32     exit('登录失败!点击此处 <a href="javascript:history.back(-1);">返回</a> 重试');
33 }
34 ?>

my.php

 1 <?php
 2 session_start();
 3 
 4 //检测是否登录,若没登录则转向登录界面
 5 if(!isset($_SESSION['userid'])){
 6     header("Location:login.html");
 7     exit();
 8 }
 9 
10 //包含数据库连接文件
11 include('conn.php');
12 $userid = $_SESSION['userid'];
13 $username = $_SESSION['username'];
14 $user_query = mysql_query("select * from user where uid=$userid limit 1");
15 $row = mysql_fetch_array($user_query);
16 echo '用户信息:<br />';
17 echo '用户ID:',$userid,'<br />';
18 echo '用户名:',$username,'<br />';
19 echo '邮箱:',$row['email'],'<br />';
20 echo '注册日期:',date("Y-m-d", $row['regdate']),'<br />';
21 echo '<a href="login.php?action=logout">注销</a> 登录<br />';
22 ?>

 

posted @ 2016-09-23 10:53  纸飞机的梦想  阅读(71113)  评论(2编辑  收藏  举报