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 ?>
相關文章
相關標籤/搜索