博客列表 >使用session完成用户跟踪

使用session完成用户跟踪

ccc9112020
ccc9112020原创
2020年12月03日 18:58:04862浏览

cookie,session,token,是用户认证和跟踪的三个主要工具。
cookie存储在浏览器端,安全性比较低,由用户控制。
session存储在服务器端,主要基于cookie。
而token在现在的移动端非常常见。
下面简单演示session的用户跟踪。

一个网站下面有index.php,login.php,check.php三个文件。

check.php:

  1. <?php
  2. $pdo=new PDO('mysql:host=localhost;dbname=phplesson','root','root');
  3. $stmt=$pdo->prepare('SELECT username,password,id FROM adminuser');
  4. $stmt->execute();
  5. $users=$stmt->fetchAll(PDO::FETCH_ASSOC);
  6. extract($_POST);
  7. var_dump($_POST);
  8. $users=array_filter($users,function($user) use ($username,$password){
  9. return $username===$user['username'] && $password === $user['password'];
  10. });
  11. // die($users);
  12. // print_r($users);
  13. // print_r($user);
  14. if(count($users)===1){
  15. echo "成功登陆";
  16. setcookie('username','',time()-3600);
  17. setcookie('auth','',time()-3600);
  18. if(!empty($auto_login)){
  19. setcookie('username',$username,strtotime("+7days"));
  20. $salt="phplesson";
  21. $auth=md5($username.$password.$salt).",".$users[0]['id'];
  22. setcookie('auth',$auth,strtotime("+7days"));
  23. }else{
  24. setcookie('username',$username);
  25. }
  26. exit("
  27. <script>
  28. alert('登陆成功');
  29. location.href='index.php';
  30. </script>
  31. ");
  32. }else{
  33. exit("
  34. <script>
  35. alert('登陆不成功');
  36. location.href='login.php';
  37. </script>
  38. ");
  39. }

login.php

  1. <?php
  2. if(isset($_GET['action'])&&$_GET['action']=='logout'){
  3. setcookie("username",);
  4. setcookie("auth","",time()-3600);
  5. }
  6. ?>
  7. <!DOCTYPE html>
  8. <html lang="">
  9. <head>
  10. <meta charset="UTF-8">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  12. <title>后台登录</title>
  13. <style>
  14. *{
  15. margin:0;
  16. padding: 0;
  17. box-sizing: border-box;
  18. }
  19. h2{
  20. margin-top: 1em;
  21. text-align: center;
  22. }
  23. h2>button{
  24. background-color: lightgreen;
  25. padding: 3px;
  26. border:none;
  27. border-radius: 3px;
  28. }
  29. form{
  30. display: grid;
  31. width: 20em;
  32. /* auto可以自动占据空间实现居中 */
  33. margin:2em auto;
  34. background-color: lightblue;
  35. padding: 1em;
  36. grid-template-columns: 5em 10em;
  37. place-content: center;
  38. gap:1em 0;
  39. border:3px solid #ccc;
  40. }
  41. form>.auto-login{
  42. color:#333333;
  43. font-size: 12px;
  44. display: flex;
  45. justify-content: space-evenly;
  46. padding: 0.3em;
  47. margin-left: -1em;
  48. align-items: center;
  49. }
  50. form>button{
  51. grid-area: auto/2/auto/span 1;
  52. background-color: lightgreen;
  53. border:none;
  54. font-size: 1.2em;
  55. letter-spacing: 0.5em;
  56. }
  57. form>button:hover{
  58. color:#333334;
  59. background-color: greenyellow;
  60. }
  61. </style>
  62. </head>
  63. <body>
  64. <h2>后台用户登录&nbsp;&nbsp;&nbsp;<button>我要注册</button></h2>
  65. <form action="check.php" method="post">
  66. <label for="username">用户名:</label>
  67. <input type="text" name="username" id="username" placeholder="用户名">
  68. <label for="password">密码:</label>
  69. <input type="password" name="password" id="password">
  70. <div class="auto-login">
  71. <input type="checkbox" name="auto_login" id="auto-login">
  72. <label for="auto-login">自动登录</label>
  73. </div>
  74. <button>登录</button>
  75. </form>
  76. </body>
  77. </html>

index.php

  1. <?php
  2. if(!isset($_COOKIE['username'])){
  3. exit("
  4. <Script>
  5. alert('请先登录');
  6. location.href='login.php';
  7. </Script>
  8. ");
  9. }
  10. if(isset($_COOKIE['auth'])){
  11. $auth=$_COOKIE['auth'];
  12. $authArr=explode(",",$auth);
  13. $is_auth=$authArr[0];
  14. $id=end($authArr);
  15. $pdo=new PDO('mysql:host=localhost;dbname=phplesson','root','root');
  16. $stmt=$pdo->prepare('SELECT username,password,id FROM adminuser WHERE id=?');
  17. $stmt->execute([$id]);
  18. $user=$stmt->fetch(PDO::FETCH_ASSOC);
  19. if($stmt->rowCount()==1){
  20. $username=$user['username'];
  21. $password=$user['password'];
  22. $salt='phplesson';
  23. $auth=md5($username.$password.$salt);
  24. if($auth!=$is_auth){
  25. exit("
  26. <Script>
  27. alert('请您先登录');
  28. location.href='login.php';
  29. </Script>
  30. ");
  31. }
  32. }else{
  33. exit("
  34. <Script>
  35. alert('请您先登录');
  36. location.href='login.php';
  37. </Script>
  38. ");
  39. }
  40. }
  41. ?>
  42. <!DOCTYPE html>
  43. <html lang="">
  44. <head>
  45. <meta charset="UTF-8">
  46. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  47. <title>后台管理系统</title>
  48. <style>
  49. header{
  50. width: 100%;
  51. height: 4em;
  52. display: flex;
  53. justify-content: space-between;
  54. background-color: lightblue;
  55. padding-right:1em;
  56. }
  57. header>.logo{
  58. padding: 1em 1em;
  59. }
  60. header>.user-status{
  61. width: 6em;
  62. padding: 1em;
  63. position: relative;
  64. }
  65. header>.user-status>.islogin{
  66. position: absolute;
  67. width: 100%;
  68. height: 100%;
  69. }
  70. header>.user-status>.unlogin{
  71. width: 100%;
  72. height: 100%;
  73. position: absolute;
  74. }
  75. header>.user-status>*{
  76. display: flex;
  77. justify-content: space-evenly;
  78. }
  79. header>.user-status>*.unactive{
  80. display: none;
  81. }
  82. .container{
  83. text-align: center;
  84. margin:2em;
  85. font-size: 3em;
  86. }
  87. </style>
  88. </head>
  89. <body>
  90. <header class="header">
  91. <div class="logo">
  92. 简书后台
  93. </div>
  94. <div class="user-status">
  95. <div class="islogin">
  96. <div class="username">
  97. <?php echo $_COOKIE['username']; ?>
  98. </div>
  99. <div class="logout" id="logout">
  100. 退出
  101. </div>
  102. </div>
  103. </div>
  104. </header>
  105. <div class="container">
  106. hello,欢迎你啊,<?php echo $_COOKIE['username']; ?>同学!
  107. </div>
  108. <script>
  109. document.querySelector('#logout').addEventListener('click',(ev)=>{
  110. if(confirm('是否退出')){
  111. window.location.assign("login.php?action=logout");
  112. }
  113. },false);
  114. </script>
  115. </body>
  116. </html>

效果图:

但是并没有看到老师界面的PHPSESSID,这不是很明白。当然了,基本功能并没有受影响。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议
灭绝师太2020-12-04 14:29:051楼
1. isset($_COOKIE['auth'])&&!empty($_COOKIE['auth']),这样判断auth是否存在就更严谨了,有且不为空 2. PHPSESSID需要启用session才有~