亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 13
粉絲 0
評(píng)論 7
訪問(wèn)量 21084
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
使用session完成用戶跟蹤
ccc9112020
原創(chuàng)
1038人瀏覽過(guò)

cookie,session,token,是用戶認(rèn)證和跟蹤的三個(gè)主要工具。
cookie存儲(chǔ)在瀏覽器端,安全性比較低,由用戶控制。
session存儲(chǔ)在服務(wù)器端,主要基于cookie。
而token在現(xiàn)在的移動(dòng)端非常常見(jiàn)。
下面簡(jiǎn)單演示session的用戶跟蹤。

一個(gè)網(wǎng)站下面有index.php,login.php,check.php三個(gè)文件。

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>后臺(tái)登錄</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可以自動(dòng)占據(jù)空間實(shí)現(xiàn)居中 */
  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>后臺(tái)用戶登錄&nbsp;&nbsp;&nbsp;<button>我要注冊(cè)</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">自動(dòng)登錄</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('請(qǐng)先登錄');
  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('請(qǐng)您先登錄');
  28. location.href='login.php';
  29. </Script>
  30. ");
  31. }
  32. }else{
  33. exit("
  34. <Script>
  35. alert('請(qǐng)您先登錄');
  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>后臺(tái)管理系統(tǒng)</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. 簡(jiǎn)書(shū)后臺(tái)
  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']; ?>同學(xué)!
  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>

效果圖:

但是并沒(méi)有看到老師界面的PHPSESSID,這不是很明白。當(dāng)然了,基本功能并沒(méi)有受影響。

批改老師:滅絕師太滅絕師太

批改狀態(tài):合格

老師批語(yǔ):
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
1條評(píng)論
滅絕師太 2020-12-04 14:29:05
1. isset($_COOKIE['auth'])&&!empty($_COOKIE['auth']),這樣判斷auth是否存在就更嚴(yán)謹(jǐn)了,有且不為空 2. PHPSESSID需要啟用session才有~
1樓
作者最新博文
關(guān)于我們 免責(zé)申明 意見(jiàn)反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)