• PHP 附录 : 用户注册与登录完整代码


     


    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{520px; margin: 0 auto;} 
    10. legend{font-weight:bold; font-size:14px;} 
    11. label{float:left; 70px; margin-left:10px;} 
    12. .left{margin-left:80px;} 
    13. .input{150px;} 
    14. span{color: #666666;} 
    15. </style> 
    16. <script language=JavaScript> 
    17. <!-- 
    18.  
    19. function InputCheck(LoginForm) 
    20. if (LoginForm.username.value == "") 
    21. alert("请输入用户名!"); 
    22. LoginForm.username.focus(); 
    23. return (false); 
    24. if (LoginForm.password.value == "") 
    25. alert("请输入密码!"); 
    26. LoginForm.password.focus(); 
    27. return (false); 
    28.  
    29. //--> 
    30. </script> 
    31. </head> 
    32. <body> 
    33. <div> 
    34. <fieldset> 
    35. <legend>用户登录</legend> 
    36. <form name="LoginForm" method="post" action="login.php" onSubmit="return InputCheck(this)"> 
    37. <p> 
    38. <label for="username" class="label">用户名:</label> 
    39. <input id="username" name="username" type="text" class="input" /> 
    40. <p/> 
    41. <p> 
    42. <label for="password" class="label">密 码:</label> 
    43. <input id="password" name="password" type="password" class="input" /> 
    44. <p/> 
    45. <p> 
    46. <input type="submit" name="submit" value=" 确 定 " class="left" /> 
    47. </p> 
    48. </form> 
    49. </fieldset> 
    50. </div> 
    51. </body> 
    52. </html>

    conn.php

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

    reg.php

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

    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{300px; margin: 0 auto;} 
    10. legend{font-weight:bold; font-size:14px;} 
    11. .label{float:left; 70px; margin-left:10px;} 
    12. .left{margin-left:80px;} 
    13. .input{150px;} 
    14. span{color: #666666;} 
    15. </style> 
    16. <script language=JavaScript> 
    17. <!-- 
    18.  
    19. function InputCheck(LoginForm) 
    20. if (LoginForm.username.value == "") 
    21. alert("请输入用户名!"); 
    22. LoginForm.username.focus(); 
    23. return (false); 
    24. if (LoginForm.password.value == "") 
    25. alert("请输入密码!"); 
    26. LoginForm.password.focus(); 
    27. return (false); 
    28.  
    29. //--> 
    30. </script> 
    31. </head> 
    32. <body> 
    33. <div> 
    34. <fieldset> 
    35. <legend>用户登录</legend> 
    36. <form name="LoginForm" method="post" action="login.php" onSubmit="return InputCheck(this)"> 
    37. <p> 
    38. <label for="username" class="label">用户名:</label> 
    39. <input id="username" name="username" type="text" class="input" /> 
    40. <p/> 
    41. <p> 
    42. <label for="password" class="label">密 码:</label> 
    43. <input id="password" name="password" type="password" class="input" /> 
    44. <p/> 
    45. <p> 
    46. <input type="submit" name="submit" value=" 确 定 " class="left" /> 
    47. </p> 
    48. </form> 
    49. </fieldset> 
    50. </div> 
    51. </body> 
    52. </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. if(!isset($_POST['submit'])){ 
    13. exit('非法访问!'); 
    14. $username = htmlspecialchars($_POST['username']); 
    15. $password = MD5($_POST['password']); 
    16.  
    17. //包含数据库连接文件 
    18. include('conn.php'); 
    19. //检测用户名及密码是否正确 
    20. $check_query = mysql_query("select uid from user where username='$username' and password='$password' 
    21. limit 1"); 
    22. if($result = mysql_fetch_array($check_query)){ 
    23. //登录成功 
    24. $_SESSION['username'] = $username; 
    25. $_SESSION['userid'] = $result['uid']; 
    26. echo $username,' 欢迎你!进入 <a href="my.php">用户中心</a><br />'; 
    27. echo '点击此处 <a href="login.php?action=logout">注销</a> 登录!<br />'; 
    28. exit; 
    29. else { 
    30. exit('登录失败!点击此处 <a href="javascript:history.back(-1);">返回</a> 重试'); 
    31. ?> 


    my.php

    1. <?php 
    2. session_start(); 
    3.  
    4. //检测是否登录,若没登录则转向登录界面 
    5. if(!isset($_SESSION['userid'])){ 
    6. header("Location:login.html"); 
    7. exit(); 
    8.  
    9. //包含数据库连接文件 
    10. include('conn.php'); 
    11. $userid = $_SESSION['userid']; 
    12. $username = $_SESSION['username']; 
    13. $user_query = mysql_query("select * from user where uid=$userid limit 1"); 
    14. $row = mysql_fetch_array($user_query); 
    15. echo '用户信息:<br />'; 
    16. echo '用户ID:',$userid,'<br />'; 
    17. echo '用户名:',$username,'<br />'; 
    18. echo '邮箱:',$row['email'],'<br />'; 
    19. echo '注册日期:',date("Y-m-d", $row['regdate']),'<br />'; 
    20. echo '<a href="login.php?action=logout">注销</a> 登录<br />'; 
    21. ?>
     
     
  • 相关阅读:
    django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
    Error fetching command 'collectstatic': You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path. Command 'collectstatic' skipped
    windows 虚拟环境下 安装 mysql 引擎一系列错误处理
    项目概念流程
    pip 使用
    HTTPserver v3.0 版本项目
    GitHub 使用
    git 操作命令详解
    git 忽略部分文件类型的同步
    Python 正则处理_re模块
  • 原文地址:https://www.cnblogs.com/u0mo5/p/4168044.html
Copyright © 2020-2023  润新知