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

搜索
博主信息
博文 56
粉絲 1
評論 0
訪問量 76335
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
pdo類的登錄驗(yàn)證使用cookie自動登錄
零龍
原創(chuàng)
903人瀏覽過

POD類的登錄驗(yàn)證使用cookie自動登錄

  • 編寫config.php、login.php、check.php、index.php 四個(gè)文件。

    config.php使用POD連接mysql數(shù)據(jù)庫

    代碼示例
  1. <?php
  2. define('DB_HOST','localhost');//主機(jī)名
  3. define('DB_USER','root');//鏈接數(shù)據(jù)庫用戶名
  4. define('DB_PWD','142536');//鏈接數(shù)據(jù)庫密碼
  5. define('DB_NAME','mysqli');//數(shù)據(jù)庫名稱
  6. define('DB_PORT','3306');//數(shù)據(jù)庫端口
  7. define('DB_TYPE','mysql');//數(shù)據(jù)庫型號
  8. define('DB_CHARSET','utf8');//數(shù)據(jù)庫編碼方式
  9. define('DB_DSN',DB_TYPE.":host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET);
  10. //定義PDO的DSN,數(shù)據(jù)源名,包括主機(jī)名,端口號和數(shù)據(jù)庫名稱
  11. $dsn='mysql:host=localhost;dbname=mysqli';
  12. //PDO驅(qū)動程序的名稱,后面為一個(gè)冒號,再后面是可選的驅(qū)動程序鏈接數(shù)據(jù)庫變量信息,如主機(jī)名,端口和數(shù)據(jù)名
  13. try
  14. {
  15. $pdo =new PDO(DB_DSN,DB_USER,DB_PWD);
  16. }
  17. catch(PDOException $e)
  18. {
  19. echo $e->getMessage();
  20. //捕捉特定于數(shù)據(jù)庫信息的PDOException 異常
  21. }
  22. catch(Throwable $e)
  23. {
  24. echo $e->getMessage();
  25. //捕捉擁有Throwable 接口的錯(cuò)誤或者其他異常
  26. }

login.php文件用戶登錄輸入信息

代碼示例

  1. <?
  2. require"config.php";
  3. if($_GET["action"] == "logOut")
  4. {
  5. if(isset($_COOKIE['auto']))
  6. {
  7. exit("
  8. <script>
  9. alert('期待您的下次光臨:".$_COOKIE['username']."');
  10. window.close();
  11. </script>
  12. ");
  13. }
  14. else
  15. {
  16. setcookie("username"," ",time()-3600);
  17. setcookie("auth"," ",time()-3600);
  18. }
  19. }
  20. else
  21. {
  22. $auth = $_COOKIE['auth'];
  23. $res=explode("%",$auth);
  24. $id = end($res);
  25. $sql ="SELECT `username`,`password`,`id` FROM `user` WHERE `id`=?";
  26. $stmt = $pdo->prepare($sql);
  27. $stmt->bindParam(1,$id);
  28. $stmt->execute();
  29. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  30. $rs = $stmt->rowCount();
  31. if($rs==1)
  32. {
  33. exit("
  34. <script>
  35. alert('歡迎您的歸來:".$_COOKIE['username']."');
  36. location.href = 'index.php';
  37. </script>
  38. ");
  39. }
  40. }
  41. ?>
  42. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  43. <html xmlns="http://www.w3.org/1999/xhtml">
  44. <head>
  45. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  46. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  47. <title>用戶登錄</title>
  48. <link href="../cookie/lx/css/style.css" rel="stylesheet" type="text/css" media="all" />
  49. <script type="text/javascript" src="../cookie/lx/js/jquery.min.js"></script>
  50. <script type="text/javascript">
  51. $(document).ready(function(c) {
  52. $('.alert-close').on('click', function(c){
  53. $('.message').fadeOut('slow', function(c){
  54. $('.message').remove();
  55. });
  56. });
  57. });
  58. </script>
  59. </head>
  60. <body>
  61. <div class="message warning">
  62. <div class="inset">
  63. <div class="login-head">
  64. <h1>用戶登錄</h1>
  65. <div class="alert-close"></div>
  66. </div>
  67. <form action="check.php" method="POST" name="form">
  68. <ul>
  69. <li><input type="text" name="username" class="text" value="用戶名" onFocus="this.value = '';" onBlur="if (this.value == '') {this.value = '用戶名';}"><a href="#" class=" icon user"></a></li>
  70. <li><input type="password" name="password" value="******" onFocus="this.value = '';" onBlur="if (this.value == '') {this.value = '******';}"> <a href="#" class="icon lock"></a></li>
  71. </ul>
  72. <div class="submit">
  73. <input type="submit" onClick="myFunction()" value="登錄" >
  74. <h4><input type="checkbox" name="auto">記住密碼</h4>
  75. <div class="clear"> </div>
  76. </div>
  77. </form>
  78. </div>
  79. </div>
  80. </body>
  81. </html>

示例圖

check.php 處理用戶信息是否正確

代碼示例

  1. <?php
  2. require "config.php";
  3. $username = $_POST['username'];
  4. $password = md5($_POST['password']);
  5. $auto =$_POST['auto'];
  6. $sql ="SELECT `username`,`password`,`id` FROM `user` WHERE `username`=? AND `password` = ?";
  7. $stmt= $pdo->prepare($sql);
  8. $stmt->bindParam(1,$username);
  9. $stmt->bindParam(2,$password);
  10. $stmt->execute();
  11. $res=$stmt->fetch(PDO::FETCH_ASSOC);
  12. if($stmt->rowCount()==1){
  13. setcookie("username","",time()-3600);
  14. setcookie("auth","",time()-3600);
  15. if(!empty($auto))
  16. {
  17. setcookie("username",$username,strtotime("+7 days"));
  18. $salt="ynllww";
  19. $auth= md5($username.$password.$salt)."%".$res['id'];
  20. setcookie("auth",$auth,strtotime("+7 days"));
  21. setcookie("auto",$auto,strtotime("+7 days"));
  22. header("Location:index.php");
  23. }
  24. else
  25. {
  26. setcookie("username",$username);
  27. $salt="ynllww";
  28. $auth= md5($username.$password.$salt)."%".$res['id'];
  29. setcookie("auth",$auth);
  30. header("Location:index.php");
  31. }
  32. }
  33. else
  34. {
  35. exit ("<script>alert('用戶名密碼錯(cuò)誤');location.href='login.php';</script>");
  36. }
  37. ?>

示例圖

index.php 用戶輸入信息正確進(jìn)入

示例

  1. <?php
  2. require "config.php";
  3. $auth = $_COOKIE['auth'];
  4. $res=explode("%",$auth);
  5. $id = end($res);
  6. $auth_real=$res[0];
  7. //end 取數(shù)組最后一個(gè)成員
  8. $sql = "SELECT `username`,`password`,`id` FROM `user` WHERE `id`=?";
  9. $stmt = $pdo->prepare($sql);
  10. $stmt->bindParam(1,$id);
  11. $stmt->execute();
  12. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  13. if($stmt->rowCount()==1)
  14. {
  15. $username = $res['username'];
  16. $password = $res['password'];
  17. $salt="ynllww";
  18. $auth = md5($username.$password.$salt);
  19. if($auth != $auth_real)
  20. {
  21. exit("
  22. <script>
  23. alert('登錄錯(cuò)誤,請先登錄!');
  24. location.href = 'login.php';
  25. </script>
  26. ");
  27. }
  28. }
  29. else
  30. {
  31. exit("
  32. <script>
  33. alert('登錄錯(cuò)誤,請先登錄!');
  34. location.href = 'login.php';
  35. </script>
  36. ");
  37. }
  38. ?>
  39. <!DOCTYPE html>
  40. <html lang="en">
  41. <head>
  42. <meta charset="UTF-8">
  43. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  44. <title>登錄成功</title>
  45. </head>
  46. <style>
  47. * {
  48. padding: 0;
  49. margin: 0;
  50. box-sizing: border-box;
  51. }
  52. h2
  53. {
  54. text-align: center;
  55. margin-top:20px ;
  56. margin-bottom: 20px;
  57. }
  58. a {
  59. text-decoration: none;
  60. color: #CCC;
  61. }
  62. nav
  63. {
  64. height: 50px;
  65. background-color: #333;
  66. padding: 0 30px;
  67. display: flex;
  68. flex-flow: row nowrap;
  69. }
  70. nav a{
  71. height: inherit;
  72. line-height: 50px;
  73. padding: 0 15px;
  74. justify-content: center;
  75. }
  76. nav a:last-child
  77. {
  78. margin-left: auto;
  79. }
  80. nav a:hover{
  81. background-color: seagreen;
  82. color: white;
  83. }
  84. </style>
  85. <body>
  86. <h2>歡迎<?php echo $_COOKIE['username']; ?>光臨</h2>
  87. <nav>
  88. <a href="">首頁</a>
  89. <a href="">待付款的寶貝</a>
  90. <a href="">待收貨的寶貝</a>
  91. <a href="">已購買的寶貝</a>
  92. <a href="login.php?action=logOut">安全退出</a>
  93. </nav>
  94. </body>
  95. </html>

示例圖

  • 寫過程中使用了PDO對象連接mysql數(shù)據(jù)庫,重點(diǎn)學(xué)習(xí)使用了PDO類的prepare()、
    bindParam()、execute()。
  • 在用戶正確輸入信息切選擇自動登錄后再Cookie記錄,再用戶下次訪問的時(shí)候不需要輸入信息直接進(jìn)行index.php。
  • 在login.php使用兩個(gè)嵌套對用戶的Cookie進(jìn)行判斷,
  • 用戶第一次訪問直接顯示輸入信息頁面,用戶在沒選擇自動登錄,下次登錄需輸入用戶信息,
  • 用戶選擇自動登錄后,在login.php進(jìn)行判斷$auto是否被勾選,勾選了直接登錄到index.php
批改老師:天蓬老師天蓬老師

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

老師批語:唯一亮點(diǎn)可能就是登錄窗口比較好看
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報(bào)處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務(wù)協(xié)議
0條評論
作者最新博文
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

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

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