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

搜索
博主信息
博文 32
粉絲 2
評論 0
訪問量 37120
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
防止cookie欺騙,實現(xiàn)7天自動登錄
簡行
原創(chuàng)
1301人瀏覽過

1.checkLogin.php:驗證登錄文件

  1. <?php
  2. header('content-type:text/html;charset=utf-8');
  3. require './config.php';
  4. $username = $_POST['uname'];
  5. $password = md5($_POST['pwd']);
  6. $islogin = $_POST['islogin'];
  7. $sql = "SELECT * FROM `mu_user` WHERE `username`=? AND `password`=? ";
  8. $stm = $pdo -> prepare($sql);
  9. $stm ->bindParam(1,$username);
  10. $stm ->bindParam(2,$password);
  11. $stm ->execute();
  12. $res = $stm->fetch(PDO::FETCH_ASSOC);
  13. if($stm->rowCount() == 1){
  14. //驗證成功
  15. clearCookie();
  16. if($islogin==1){
  17. //記住密碼
  18. setcookie("username",$res['username'],strtotime('+7 days'));
  19. $token = settoken($res['username'],$res['password'],$res['id']);
  20. setcookie("token",$token,strtotime('+7 days'));
  21. }else{
  22. // 無記住密碼
  23. setcookie("username",$res['username']);
  24. $token = settoken($res['username'],$res['password'],$res['id']);
  25. setcookie("token",$token);
  26. }
  27. exit("
  28. <script>
  29. alert('登錄成功!');
  30. location.href ='index.php';
  31. </script>
  32. ");
  33. }else{
  34. //驗證失敗
  35. exit("
  36. <script>
  37. alert('用戶名或密碼有誤!');
  38. location.href ='login.php';
  39. </script>
  40. ");
  41. }
  42. //清除cookie
  43. function clearCookie(){
  44. setcookie("username",'',time()-1800);
  45. setcookie("token",'',time()-1800);
  46. }
  47. //設置token
  48. function settoken($username,$password,$id)
  49. {
  50. $salk = "czx";
  51. $token = md5($salk.$username.$password)."*".$id;
  52. return $token;
  53. }

2.config.php:數(shù)據(jù)庫配置文件及連接

  1. <?php
  2. //主機地址
  3. define("DB_HOST","localhost");
  4. //數(shù)據(jù)庫用戶名
  5. define("DB_USER","root");
  6. //數(shù)據(jù)庫密碼
  7. define("DB_PASSWORD","root123");
  8. // 數(shù)據(jù)庫型號
  9. define("DB_TYPE","mysql");
  10. // 數(shù)據(jù)庫名稱
  11. define("DB_NAME","my_user");
  12. //數(shù)據(jù)庫編碼
  13. define('DB_CHARSET', 'utf8');
  14. //數(shù)據(jù)庫端口號
  15. define('DB_PORT', '3306');
  16. //定義PDO的DSN,數(shù)據(jù)源名,包括主機名,端口號和數(shù)據(jù)庫名稱。
  17. define('DSN', DB_TYPE.":host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET);
  18. try{
  19. //連接數(shù)據(jù)款
  20. $pdo = new PDO(DSN,DB_USER,DB_PASSWORD);
  21. } catch(PDOException $e){
  22. //捕捉特定于數(shù)據(jù)庫信息的PDOEXCEPTION 異常
  23. echo $e->getMessage();
  24. } catch(Throwable $e){
  25. //捕捉擁有Throwable接口的錯誤或者其他異常
  26. echo $e->getMessage();
  27. }

3.login.php:登錄頁面文件

  1. <?php
  2. if($_GET['act'] == 'out'){
  3. setcookie("username",'',time()-1800);
  4. setcookie("token",'',time()-1800);
  5. }
  6. $token = $_COOKIE['token'];
  7. $username = $_COOKIE['username'];
  8. if(!empty($username) &&!empty($token)&& ($_GET['act'] != 'out')){
  9. exit("
  10. <script>
  11. alert('用戶已登錄,請直接訪問!');
  12. location.href ='index.php';
  13. </script>
  14. ");
  15. }
  16. ?>
  17. <!DOCTYPE html>
  18. <html lang="en">
  19. <head>
  20. <meta charset="UTF-8">
  21. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  22. <title>登錄</title>
  23. </head>
  24. <style>
  25. *{
  26. margin: 0px;
  27. padding: 0;
  28. }
  29. .contater {
  30. border: 1px solid #000;
  31. width: 300px;
  32. display: flex;
  33. flex-direction: column;
  34. margin: auto;
  35. }
  36. .contater>div {
  37. margin-top: 20px;
  38. }
  39. h3 {
  40. text-align: center;
  41. }
  42. .contater > .submit >input{
  43. margin: 15px 140px;
  44. font-size: 1.5rem;
  45. }
  46. </style>
  47. <body>
  48. <h3 >登錄</h3>
  49. <form action="checkLogin.php" method="post">
  50. <div class="contater">
  51. <div>
  52. <span>用戶名:</span>
  53. <input type="text" name="uname">
  54. </div>
  55. <div>
  56. <span>密&nbsp;&nbsp;&nbsp;碼:</span>
  57. <input type="password" name="pwd">
  58. </div>
  59. <div>
  60. <input type="radio"" name="islogin" value="1">
  61. <span>記住密碼</span>
  62. </div>
  63. <div class="submit">
  64. <input type="submit" value="登錄">
  65. </div>
  66. </div>
  67. </form>
  68. </body>
  69. </html>

4.index.php:首頁文件

  1. <?php
  2. $token = $_COOKIE['token'];
  3. $token_arr = explode("*",$token);
  4. $uid = end($token_arr);//獲取用戶id
  5. require "./config.php";
  6. $sql = "SELECT * FROM `mu_user` WHERE `id`=?";
  7. $stm = $pdo ->prepare($sql);
  8. $stm ->bindParam(1,$uid);
  9. $stm ->execute();
  10. $result =$stm->fetch(PDO::FETCH_ASSOC);
  11. if($stm->rowCount()==1){
  12. $salk = "czx";
  13. $token_res = md5($salk.$result['username'].$result['password']);
  14. if($token_res != $token_arr[0]){
  15. exit("
  16. <script>
  17. alert('請先登錄');
  18. loction.href ='login.php';
  19. </script>
  20. ");
  21. }
  22. }else{
  23. exit("
  24. <script>
  25. alert('請您先登錄');
  26. location.href='login.php';
  27. </script>
  28. ");
  29. }
  30. ?>
  31. <!DOCTYPE html>
  32. <html lang="en">
  33. <head>
  34. <meta charset="UTF-8" />
  35. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  36. <title>首頁</title>
  37. </head>
  38. <style>
  39. * {
  40. margin: 0px;
  41. padding: 0px;
  42. box-sizing: border-box;
  43. }
  44. h1 {
  45. text-align: center;
  46. }
  47. a {
  48. text-decoration: none;
  49. font-size: 1.5rem;
  50. color: darkgray;
  51. }
  52. a:hover {
  53. background-color: lightskyblue;
  54. border-radius: 5%;
  55. /* font-size: 2rem; */
  56. }
  57. li {
  58. list-style-type: none;
  59. color: darkgray;
  60. }
  61. span {
  62. color: darkgray;
  63. font-size: 1.5rem;
  64. margin-right: 15px;
  65. color:burlywood
  66. }
  67. .top {
  68. /* width: 960px; */
  69. background-color: linen;
  70. display: flex;
  71. flex-flow: row nowrap;
  72. justify-content: space-between;
  73. }
  74. .top > div {
  75. margin: 10px 40px;
  76. }
  77. .column {
  78. /* width: 960px; */
  79. display: flex;
  80. flex-flow: row nowrap;
  81. justify-content: space-around;
  82. }
  83. .column > li {
  84. margin-right: 65px;
  85. padding: 0px 20px;
  86. }
  87. </style>
  88. <body>
  89. <h1>陶轉轉首頁</h1>
  90. <div class="top">
  91. <div>
  92. <ul class="column">
  93. <li><a href="">LOGO</a></li>
  94. <li><a href="">首頁</a></li>
  95. <li><a href="">分類一</a></li>
  96. </ul>
  97. </div>
  98. <div>
  99. <span>歡迎您,<?php echo $result['username'];?></span>
  100. <a href="./login.php?act=out">退出</a>
  101. </div>
  102. </div>
  103. </body>
  104. </html>

批改老師:天蓬老師天蓬老師

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

老師批語:沒有絕對安全的登錄,但我們可以做到盡可能的安全
本博文版權歸博主所有,轉載請注明地址!如有侵權、違法,請聯(lián)系admin@php.cn舉報處理!
全部評論 文明上網理性發(fā)言,請遵守新聞評論服務協(xié)議
0條評論
關于我們 免責申明 意見反饋 講師合作 廣告合作 最新更新
php中文網:公益在線php培訓,幫助PHP學習者快速成長!
關注服務號 技術交流群
PHP中文網訂閱號
每天精選資源文章推送
PHP中文網APP
隨時隨地碎片化學習
PHP中文網抖音號
發(fā)現(xiàn)有趣的

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

  • 登錄PHP中文網,和優(yōu)秀的人一起學習!
    全站2000+教程免費學