PHP 附錄 : 使用者註冊與登入完整代碼

來源:互聯網
上載者:User

標籤:style   http   ar   io   color   os   sp   for   java   

 
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. ?>
  

社交

PHP 附錄 : 使用者註冊與登入完整代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.