博客列表 >登录功能实战

登录功能实战

王佳祥
王佳祥原创
2020年07月18日 15:49:40612浏览

登录功能实战

一、前端表单页面login.php

  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. body{
  9. padding:0;
  10. margin: 0;
  11. box-sizing: border-box;
  12. display: flex;
  13. justify-content: center;
  14. background-image: url('apic3595.jpg');
  15. background-size: 100%;
  16. font-size: 30px;
  17. }
  18. .table{
  19. width: 600px;
  20. height: 400px;
  21. background: #fff;
  22. align-self: center;
  23. border-radius: 10px;
  24. box-shadow: 0 0 10px #888;
  25. position: absolute;
  26. top:30%;
  27. padding: 20px;
  28. text-align: center;
  29. }
  30. #username,#password{
  31. width: 100%;
  32. height: 40px;
  33. font-size: 20px;
  34. }
  35. #remember{
  36. width: 20px;
  37. height: 20px;
  38. border: 1px solid red;
  39. }
  40. .td-1{
  41. text-align: start;
  42. }
  43. #submit{
  44. width: 80%;
  45. height: 40px;
  46. font-size: 25px;
  47. border: 0;
  48. }
  49. #submit:hover{
  50. background: #66CC99;
  51. cursor:pointer;
  52. }
  53. </style>
  54. </head>
  55. <body>
  56. <table class="table">
  57. <form action="enter.php" method="POST" onsubmit="return checkForm()">
  58. <tr>
  59. <td>账号:</td>
  60. <td><input type="text" name="username" id="username" placeholder="请输入您的账号" onblur="checkUser()"></td>
  61. </tr>
  62. <tr>
  63. <td>密码:</td>
  64. <td><input type="password" name="password" id="password" placeholder="请输入您的密码" onblur="checkPwd()"></td>
  65. </tr>
  66. <tr>
  67. <td><input type="checkbox" name="remember" value="1" id="remember"></td>
  68. <td class="td-1">记住密码</td>
  69. </tr>
  70. <tr>
  71. <td colspan="2"><input type="submit" value="登录" id="submit"></td>
  72. </tr>
  73. </form>
  74. </table>
  75. </body>
  76. <script>
  77. function checkUser(){
  78. $username = document.getElementById("username");
  79. if($username.value == ''){
  80. alert('请输入您的账号');
  81. return false;
  82. }else{
  83. return true;
  84. }
  85. }
  86. function checkPwd(){
  87. $password = document.getElementById("password");
  88. if($password.value == ''){
  89. alert('请输入您的密码');
  90. return false;
  91. }else{
  92. return true;
  93. }
  94. }
  95. function checkForm(){
  96. if(checkUser() == true && checkPwd() == true){
  97. return true;
  98. } else{
  99. return false;
  100. }
  101. }
  102. </script>
  103. </html>
  104. <?php
  105. //如果退出的话就清除cookie
  106. if($_GET['action'] == 'out'){
  107. setcookie('username','',time()-3600);
  108. setcookie('auth','',time()-3600);
  109. }else{
  110. $user = $_COOKIE['username'];
  111. //echo $user;
  112. $pwd = $_COOKIE['password'];
  113. if($user != ''){
  114. require "check.php";
  115. //print_r($res);
  116. //如果数据库中查询到了相应数据,就生成令牌
  117. if($stmt->rowCount() == 1){
  118. $username =$res['username'];
  119. $password = $res['password'];
  120. $string = "php.cn";
  121. $auth = md5($username.$password.$string);
  122. //var_dump($auth);
  123. //如果在数据库中查询到的数据生成令牌后和cookie中的令牌不一样
  124. //说明数据库已经被修改过,不能让他登录
  125. if($auth_real == $auth){
  126. exit("
  127. <script>
  128. if(confirm('你已经登录,是否直接登录?')){
  129. location.href='index.php';
  130. }else{
  131. location.href='login.php';
  132. }
  133. </script>
  134. ");
  135. }
  136. }
  137. }
  138. }
  139. ?>

二、验证提交数据的页面enter.php

  1. <?php
  2. require "config.php";
  3. //设置页面字符集
  4. header("Content-Type:text/html;charset=utf-8");
  5. //获取到表单传过来的账号密码
  6. $username = $_POST['username'];
  7. $password = md5($_POST['password']);
  8. //查看传过来的账号密码
  9. //echo '账号:'.$username.'<br>密码:'.$password.'<br>';
  10. //判断是否有勾选记住密码
  11. $remember = $_POST['remember'];
  12. echo '<br>'.$remember.'<br>';
  13. //连接数据库,把写好的数据库文件直接引入过来
  14. try{
  15. //连接数据库
  16. $pdo = new PDO(DB_DSN,DB_USER,DB_PWD,[PDO::ATTR_ERRMODE=>PDO::ERRMODE_WARNING]);
  17. }catch(PDOException $e){
  18. //输出连接错误
  19. echo $e->getMessage();
  20. }catch(Throwable $e){
  21. //输出接口错误
  22. echo $e->getMessage();
  23. }
  24. //要执行的SQL语句
  25. $sql = "SELECT * FROM `user` WHERE `username` = ? AND `password` = ?";
  26. //执行sql语句,因为要返回一个PDOStatemet对象,所以用prepare()
  27. $stmt = $pdo->prepare($sql);
  28. //绑定参数到变量用bindParam()
  29. $stmt->bindParam(1,$username);
  30. $stmt->bindParam(2,$password);
  31. //用ecxute()来执行sql预处理语句
  32. $stmt->execute();
  33. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  34. //查看返回结果
  35. //print_r($res);
  36. //如果返回行数=1的话,代表账号密码输入正确
  37. if($stmt->rowCount() == 1){
  38. //把正确的账号密码存储到cookie中
  39. //先清除上次的cookie
  40. setcookie("username","",time()-3600);
  41. setcookie("auth","",time()-3600);
  42. //如果勾选了记住密码,把账号密码保存7天
  43. if(!empty($remember)){
  44. //用cookie存储账号密码7天
  45. setcookie("username",$username,strtotime("+1 week"));
  46. /*为了密码的安全性,要给密码加密后再放到cookie中存储
  47. 盐:在密码学中,是指通过在密码任意固定位置插入特定的字符串,
  48. 让散列后的结果和使用原始密码的散列结果不相符,这种过程称之为“加盐”*/
  49. //加盐
  50. $string = 'php.cn';
  51. //生成令牌
  52. $auth = md5($username.$password.$string).",".$res['id'];
  53. setcookie("auth",$auth,strtotime("+1 week"));
  54. }else{
  55. //没有勾选记住密码
  56. setcookie("username",$username);
  57. //加盐
  58. $string = 'php.cn';
  59. //生成令牌
  60. $auth = md5($username.$password.$string).",".$res['id'];
  61. setcookie("auth",$auth);
  62. }
  63. //存储完cookie后跳转到Index.php
  64. exit("
  65. <script>
  66. location.href='index.php';
  67. </script>
  68. ");
  69. }else{
  70. //账号密码输入错误的话,用js弹框并提示用户账号密码输入错误,然后返回登录页面
  71. exit("
  72. <script>
  73. alert('账号或密码输入错误');
  74. location.href='login.php';
  75. </script>
  76. ");
  77. }

三、后端首页面index.php

  1. <?php
  2. require "check.php";
  3. if($stmt->rowCount() == 1){
  4. $username =$res['username'];
  5. $password = $res['password'];
  6. $string = "php.cn";
  7. $auth = md5($username.$password.$string);
  8. //var_dump($auth);
  9. //如果在数据库中查询到的数据生成令牌后和cookie中的令牌不一样
  10. //说明数据库已经被修改过,不能让他登录
  11. if($auth_real != $auth){
  12. exit("
  13. <script>
  14. alert('请您先登录');
  15. location.href='login.php';
  16. </script>
  17. ");
  18. }
  19. }else{
  20. exit("
  21. <script>
  22. alert('请您先登录');
  23. location.href='login.php';
  24. </script>
  25. ");
  26. }
  27. ?>
  28. <!DOCTYPE html>
  29. <html lang="en">
  30. <head>
  31. <meta charset="UTF-8">
  32. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  33. <title>后台首页</title>
  34. <style>
  35. body{
  36. padding: 0px;
  37. margin:0px;
  38. box-sizing: border-box;
  39. }
  40. body > ul{
  41. list-style: none;
  42. display: flex;
  43. width: 100%;
  44. height: 50px;
  45. background:#66CCFF;
  46. }
  47. ul > li{
  48. width: 200px;
  49. height: 50px;
  50. text-align: center;
  51. line-height: 50px;
  52. font-size: 30px;
  53. }
  54. ul>li:hover{
  55. background:#0066FF;
  56. }
  57. ul > li:last-of-type{
  58. margin-left: auto;
  59. margin-right:50px;
  60. }
  61. a{
  62. text-decoration: none;
  63. color:#000;
  64. }
  65. #personal{
  66. width: 150px;
  67. font-size: 25px;
  68. }
  69. li:nth-of-type(4){
  70. width: 400px;
  71. }
  72. ul > li:last-of-type >ul{
  73. padding:0;
  74. margin: 0;
  75. list-style: none;
  76. border: 1px solid #000;
  77. display: flex;
  78. flex-direction: column;
  79. position:absolute;
  80. display: none;
  81. }
  82. ul > li:last-of-type >ul>li{
  83. width: 200px;
  84. height: 50px;
  85. }
  86. ul > li:hover .ul-1{
  87. display: block;
  88. }
  89. </style>
  90. </head>
  91. <body>
  92. <ul>
  93. <li><a href="">首页</a></li>
  94. <li><a href="">购物</a></li>
  95. <li><a href="">学习</a></li>
  96. <li>欢迎<?php echo $_COOKIE['username'] ?>用户回来</li>
  97. <li>
  98. <a href="">个人中心</a>
  99. <ul class="ul-1">
  100. <li><a href="">换肤</a></li>
  101. <li><a href="">设置</a></li>
  102. <li><a href="login.php?action=out">退出</a></li>
  103. </ul>
  104. </li>
  105. </ul>
  106. </body>
  107. </html>
  • 因为这段代码重复了,所以我直接拿出来放到一个页面,直接引入
  1. <?php
  2. //引入数据库常量
  3. require "config.php";
  4. try{
  5. //连接数据库
  6. $pdo = new PDO(DB_DSN,DB_USER,DB_PWD,[PDO::ATTR_ERRMODE=>PDO::ERRMODE_WARNING]);
  7. }catch(PDOException $e){
  8. //输出连接错误
  9. echo $e->getMessage();
  10. }catch(Throwable $e){
  11. //输出接口错误
  12. echo $e->getMessage();
  13. }
  14. //
  15. //从cookie中获取auth
  16. $auth = $_COOKIE["auth"];
  17. //把字符串打散为数组
  18. $res = explode(",",$auth);
  19. //var_dump($res);
  20. //拿到数组中的最后一个元素
  21. $id = end($res);
  22. //var_dump($id);
  23. //获取id前面的内容赋值给一个变量
  24. $auth_real = $res[0];
  25. //var_dump($auth_real);
  26. //从数据库中获取
  27. $sql = "SELECT `username`,`password`,`id` FROM `user` WHERE `id` =?";
  28. $stmt = $pdo->prepare($sql);
  29. $stmt->bindParam(1,$id);
  30. $stmt->execute();
  31. $res = $stmt->fetch(PDO::FETCH_ASSOC);

五、视频演示

1.不保存密码



2.不保存密码,关掉浏览器,重新打开



3.保存密码,关掉浏览器,再次访问



4.直接访问index.php页面



5.直接访问enter.php页面



6.在勾选密码的情况下修改数据库后直接访问



六、学习总结

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议
灭绝师太2020-07-18 15:56:491楼
作业的做的很炫酷,看得出有自己的思路在里面。我来提几个点,让我们的代码更简练完美点:1.在登录页面,检测用户输入为空,return false,可以直接在input标签中,加required属性。2. php代码与html代码共存,建议php代码放在上面,代码由上往下读。3.密码不用存cookie,同样可以验证数据库密码是否被篡改(参考0717课件)。