博客列表 >PHP实战演练:登陆和注册

PHP实战演练:登陆和注册

李东亚¹⁸⁰³⁹⁵⁴⁰¹²⁰
李东亚¹⁸⁰³⁹⁵⁴⁰¹²⁰原创
2020年05月13日 13:18:24910浏览

登陆注册实战案例:

1、登陆

  1. <?php
  2. if(isset($_COOKIE['user'])){
  3. // echo 'OK';
  4. // exit('<script>alert("登陆成功!");location.href="index.php";</script>');
  5. exit('<script>alert("已经登陆,请勿重复等登陆!");location.href="index.php";</script>');
  6. }
  7. ?>
  8. <!DOCTYPE html>
  9. <html lang="en">
  10. <head>
  11. <meta charset="UTF-8">
  12. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  13. <title>用户注册</title>
  14. <style>
  15. * {
  16. margin: 0;
  17. padding: 0;
  18. }
  19. h2 {
  20. /* display: block; */
  21. width: 350px;
  22. margin: 0 auto;
  23. text-align: center;
  24. padding-top: 10px;
  25. box-sizing: border-box;
  26. }
  27. form {
  28. margin: 10px auto;
  29. width: 350px;
  30. height: 250px;
  31. background-color: #5384e8;
  32. display: flex;
  33. flex-flow: column nowrap;
  34. justify-content: space-evenly;
  35. align-content: center;
  36. align-items: center;
  37. font-size: 1.2rem;
  38. }
  39. form:hover {
  40. box-shadow: 0 0 5px #626262;
  41. }
  42. form>.button {
  43. width: 280px;
  44. display: flex;
  45. justify-content: space-evenly;
  46. }
  47. form>.button>input {
  48. width: 100px;
  49. height: 30px;
  50. background-color: #00bb00;
  51. border: none;
  52. border-radius: 15px;
  53. }
  54. form>.button>input:hover {
  55. background-color: red;
  56. color: white;
  57. }
  58. a {
  59. color: white;
  60. text-decoration: none;
  61. }
  62. </style>
  63. </head>
  64. <body>
  65. <h2>用户注册</h2>
  66. <form action="handle.php?action=select" method="POST">
  67. <!-- <fieldset> -->
  68. <!-- <legend align="center">用户注册</legend> -->
  69. <div>
  70. <label for="username">账户:</label>
  71. <input type="email" required name="username" id="username" placeholder="example@163.com">
  72. </div>
  73. <div>
  74. <label for="p2">密码:</label>
  75. <input type="password" required name="p2" id="p2" placeholder="不少于六位">
  76. </div>
  77. <!-- </fieldset> -->
  78. <div class="button">
  79. <input type="submit" value="登陆">
  80. <input type="reset" value="重置">
  81. </div>
  82. <div>
  83. <a href="regist.php">没有账号,点击此处注册!</a>
  84. </div>
  85. </form>
  86. </body>
  87. </html>

2、注册

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>用户注册</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. }
  12. h2 {
  13. /* display: block; */
  14. width: 350px;
  15. margin: 0 auto;
  16. text-align: center;
  17. padding-top: 10px;
  18. box-sizing: border-box;
  19. }
  20. form {
  21. margin: 10px auto;
  22. width: 350px;
  23. height: 450px;
  24. background-color: #5384e8;
  25. display: flex;
  26. flex-flow: column nowrap;
  27. justify-content: space-evenly;
  28. align-content: center;
  29. align-items: center;
  30. font-size: 1.2rem;
  31. }
  32. form:hover {
  33. box-shadow: 0 0 5px #626262;
  34. }
  35. form>.button {
  36. width: 280px;
  37. display: flex;
  38. justify-content: space-evenly;
  39. }
  40. form>.button>input {
  41. width: 100px;
  42. height: 30px;
  43. background-color: #00bb00;
  44. border: none;
  45. border-radius: 15px;
  46. }
  47. form>.button>input:hover {
  48. background-color: red;
  49. color: white;
  50. }
  51. a {
  52. color: white;
  53. text-decoration: none;
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <h2>用户注册</h2>
  59. <form action="handle.php?action=insert" method="POST" name="my_form">
  60. <!-- <fieldset> -->
  61. <!-- <legend align="center">用户注册</legend> -->
  62. <div>
  63. <label for="username">账户:</label>
  64. <input type="email" required name="username" id="username" placeholder="example@163.com">
  65. </div>
  66. <div>
  67. <label for="name">账户:</label>
  68. <input type="text" required name="name" id="name" placeholder="输入你的昵称">
  69. </div>
  70. <div>
  71. <label for="p1">密码:</label>
  72. <input type="password" required name="p1" id="p1" placeholder="不少于六位">
  73. </div>
  74. <div>
  75. <label for="p2">密码:</label>
  76. <input type="password" required name="p2" id="p2" placeholder="不少于六位">
  77. </div>
  78. <!-- <span id="tips" name="tips" style="color: red"></span> -->
  79. <!-- </fieldset> -->
  80. <div class="button">
  81. <input type="submit" value="注册">
  82. <input type="reset" value="重置">
  83. </div>
  84. <div>
  85. <a href="login.php">已有账号,点击此处登陆!</a>
  86. </div>
  87. </form>
  88. </body>
  89. </html>

3、首页

  1. <?php
  2. if(isset($_COOKIE['user'])){
  3. $user=unserialize($_COOKIE['user']);
  4. // var_dump($user);
  5. $username=$user['name'];
  6. $states="登出";
  7. $statesurl="handle.php?action=logout";
  8. }
  9. ?>
  10. <!DOCTYPE html>
  11. <html lang="en">
  12. <head>
  13. <meta charset="UTF-8">
  14. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  15. <title>首页</title>
  16. <style>
  17. * {
  18. margin: 0;
  19. padding: 0;
  20. }
  21. a {
  22. text-decoration: none;
  23. }
  24. nav {
  25. width: 100%;
  26. height: 40px;
  27. background: #00ffff;
  28. display: flex;
  29. justify-content: space-between;
  30. align-items: center;
  31. padding: 0 20px;
  32. box-sizing: border-box;
  33. font-size: 1.4rem;
  34. }
  35. nav a {
  36. color: white;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <nav>
  42. <a href="">首页</a>
  43. <div><a href="<?php echo null?? "regist.php";?>"><?php echo $username ?? "注册"; ?></a>&nbsp;&nbsp;&nbsp;<a
  44. href="<?php echo $statesurl ?? "login.php"; ?>"><?php echo $states ?? "登陆";?></a>
  45. </div>
  46. </nav>
  47. </body>
  48. </html>

4、登陆注册处理脚本

  1. <?php
  2. // 连接数库所需参数
  3. $dsn="mysql:host=php.edu;dbname=php.edu;charset=utf8;port=3306";
  4. $db=new PDO($dsn,'phpedu','123456');
  5. // var_dump($db);
  6. $action=filter_input(INPUT_GET,'action',FILTER_SANITIZE_STRING);
  7. $action=strtolower($action);
  8. switch($action){
  9. case 'select':
  10. // 获取查询条件
  11. // echo $_POST['username'];
  12. $user=filter_input(INPUT_POST,'username',FILTER_SANITIZE_EMAIL);
  13. $user=trim($user);
  14. // echo $user;
  15. // 准备sql语句
  16. $sql="select `id`,`password`,`name` FROM `users` WHERE `email`='{$user}'";
  17. // 预处理sql语句
  18. $stmt=$db->prepare($sql);
  19. // var_dump($stmt);
  20. $stmt->execute();
  21. $res=$stmt->fetch(PDO::FETCH_ASSOC);
  22. $password=filter_input(INPUT_POST,'p2',FILTER_SANITIZE_STRING);
  23. // echo md5($password),'<br>',$res['password'];
  24. if(md5($password)===$res['password']){
  25. setcookie('user',serialize($res),time()+7200);
  26. exit('<script>alert("登陆成功!");location.href="index.php";</script>');
  27. }else{
  28. exit('<script>alert("密码账号错误!");location.herf="login.php";</script>');
  29. }
  30. // echo md5('12345678');
  31. // printf("<pre>%s</pre>",print_r($res,true));
  32. break;
  33. case 'logout':
  34. setcookie('user',null,time()-3600);
  35. exit('<script>alert("登处成功!");location.href="index.php";</script>');
  36. break;
  37. case 'insert':
  38. $arg=[
  39. 'username'=>FILTER_SANITIZE_EMAIL,
  40. 'p1'=>FILTER_SANITIZE_STRING,
  41. 'name'=>FILTER_SANITIZE_STRING,
  42. 'p2'=>FILTER_SANITIZE_STRING
  43. ];
  44. $user=filter_input_array(INPUT_POST,$arg);
  45. if($user['p1']!==$user['p2']){
  46. exit('<script>alert("密码不一致!");location.href="regist.php";</script>');
  47. }
  48. $user['p1']=md5($user['p1']);
  49. $data=array_values($user);
  50. array_pop($data);
  51. array_push($data,time());
  52. $sql="insert `users` SET `email`='{$data[0]}',`password`='{$data[1]}',`name`='{$data[2]}',`datetime`={$data[3]}";
  53. // 处理用户数据符合写入数据库要求
  54. // printf("<pre>%s</pre>",print_r($data,true));
  55. // 预处理sql语句;
  56. $stmt=$db->prepare($sql);
  57. // var_dump($stmt);
  58. $stmt->execute();
  59. if($stmt->rowCount()===1){
  60. setcookie('user',serialize($user),time()+3600);
  61. exit('<script>alert("登陆成功!");location.href="index.php";</script>');
  62. }
  63. break;
  64. default:
  65. exit('<script>alert("未知错误!");location.href="index.php";</script>');
  66. }
  67. $db=null;

5、运行结果


二、SESSION形式(每一次对session处理都必须开启会话);

  1. session_start();//开启会话
  2. $_SESSION['user']=$user;//把用户信息写入session
  3. if(isset($_SESSION['user'])){
  4. //判断是否登陆,并作出处理
  5. }
  6. //登出操作
  7. session_distroy();

总结:

1、重点数据库连接:PDO连接、select、insert;
2、filter_input(INPUT_GET|INPUT_POST,”获取的值的name属性”,过滤类型);
3、cookie相关知识点:
(1)、setcookie(‘name’,’值信息’,’失效时间’);
(2)、读取cookie信息:$_COOKIE[‘变量名’];
(3)、删除cookie信息:setcookie(‘name’,null,time()-1);
4、session相关知识点:
(1)、session_start();开启会话,调用session必须先开启会话
(2)、读取session信息:$_SESSION[‘变量名’];
(3)、结束当前会话删除session会话信息:session_destroy();
unset($_SESSION[‘’]);注销单个会话
5、serialize()unserialize():序列化和发序列化;

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议