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

搜索
博主信息
博文 48
粉絲 3
評論 1
訪問量 44907
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
使用session會話來實現(xiàn)登錄注冊案例
吳長清
原創(chuàng)
644人瀏覽過

index.php 入口文件

  1. <?php
  2. namespace login;
  3. // 開啟會話
  4. session_start();
  5. // 判斷是否已登錄?
  6. if (isset($_SESSION['user'])) {
  7. // 反序列化
  8. $user = unserialize($_SESSION['user']);
  9. }
  10. ?>
  11. <!DOCTYPE html>
  12. <html lang="zh-CN">
  13. <head>
  14. <meta charset="UTF-8">
  15. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  16. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  17. <title>首頁/入口文件</title>
  18. <style>
  19. nav {
  20. height: 40px;
  21. background-color: deepskyblue;
  22. padding: 0 20px;
  23. display: flex;
  24. justify-content: space-between;
  25. align-items: center;
  26. }
  27. nav .loginbox {
  28. display: flex;
  29. padding: 0 20px;
  30. justify-content: space-between;
  31. align-items: center;
  32. gap: 0px 20px;
  33. }
  34. nav .loginbox>a,
  35. nav .loginbox>span {
  36. color: white;
  37. text-decoration: none;
  38. }
  39. nav .loginbox>span {
  40. font-weight: bold;
  41. }
  42. nav .loginbox>a:hover {
  43. cursor: pointer;
  44. }
  45. </style>
  46. </head>
  47. <body>
  48. <nav>
  49. <a href="index.php">我的博客</a>
  50. <div class="loginbox">
  51. <?php if (isset($user)) : ?>
  52. <span>用戶昵稱: <?= $user[0]['name'] ?></span>
  53. <span>用戶性別: <?= $user[0]['sex'] ? '女' : '男' ?></span>
  54. <a id="logout">退出</a>
  55. <?php else : ?>
  56. <a href="login.php">登錄</a>
  57. <?php endif ?>
  58. </div>
  59. </nav>
  60. <script>
  61. // 為退出按鈕創(chuàng)建事件監(jiān)聽器
  62. document.querySelector('#logout').addEventListener('click', function(event) {
  63. if (confirm('是否退出')) {
  64. // 禁用默認行為, 其實就是禁用原<a>標簽的點擊跳轉行為,使用事件中的自定義方法處理
  65. event.preventDefault();
  66. // 跳轉到退出事件處理器
  67. window.location.assign('handle.php?action=logout');
  68. }
  69. });
  70. </script>
  71. </body>
  72. </html>

login.php 登錄界面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>用戶登錄</title>
  6. <style>
  7. body {
  8. background: linear-gradient(100deg, white, #00d5ff)
  9. }
  10. fieldset {
  11. width: 300px;
  12. background: linear-gradient(100deg, #00d5ff, #00aaff);
  13. margin: 200px auto;
  14. }
  15. legend {
  16. background-color: white;
  17. margin: auto;
  18. font-size: larger;
  19. }
  20. fieldset>div {
  21. width: 240px;
  22. margin: auto;
  23. padding: 10px;
  24. }
  25. fieldset>div form div {
  26. margin-top: 5px;
  27. }
  28. button {
  29. width: 80px;
  30. }
  31. button:hover {
  32. cursor: pointer;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <fieldset>
  38. <legend>用戶登錄</legend>
  39. <div>
  40. <form action="handle.php?action=login" method="post">
  41. <div>
  42. <label for="email">郵箱:</label>
  43. <input type="email" name="email" id="email" placeholder="demo@email.com" required autofocus>
  44. </div>
  45. <div>
  46. <label for="password">密碼:</label>
  47. <input type="password" name="password" id="password" placeholder="不少于6位" required>
  48. </div>
  49. <div>
  50. <button>提交</button>
  51. </div>
  52. </form>
  53. <a href="register.php">還沒有帳號, 注冊一個吧</a>
  54. </div>
  55. </fieldset>
  56. </body>
  57. </html>

register.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. <!-- <link rel="stylesheet" type="text/css" href="css/style.css"> -->
  7. <title>注冊用戶</title>
  8. <style>
  9. body {
  10. background: linear-gradient(100deg, white, #00d5ff)
  11. }
  12. fieldset {
  13. width: 300px;
  14. background: linear-gradient(100deg, #00d5ff, #00aaff);
  15. margin: 200px auto;
  16. }
  17. legend {
  18. background-color: white;
  19. margin: auto;
  20. font-size: larger;
  21. }
  22. fieldset>div {
  23. width: 240px;
  24. margin: auto;
  25. padding: 10px;
  26. }
  27. fieldset>div form div {
  28. margin-top: 5px;
  29. }
  30. button {
  31. width: 80px;
  32. }
  33. button:hover {
  34. cursor: pointer;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <fieldset>
  40. <legend>用戶注冊</legend>
  41. <div>
  42. <form action="handle.php?action=register" method="post" onsubmit="return compare()">
  43. <div>
  44. <label for="name">呢稱:</label>
  45. <input type="text" name="name" id="name" placeholder="不少于3個字符" required autofocus>
  46. </div>
  47. <div>
  48. <label for="email">郵箱:</label>
  49. <input type="email" name="email" id="email" placeholder="demo@email.com" required>
  50. </div>
  51. <div>
  52. <label for="p1">密碼:</label>
  53. <input type="password" name="p1" id="p1" placeholder="不少于6位" required>
  54. </div>
  55. <div>
  56. <label for="p2">重復:</label>
  57. <input type="password" name="p2" id="p2" placeholder="必須與上面一致" required>
  58. </div>
  59. <div>
  60. <button>提交</button><span id="tips" style="color: red"></span>
  61. </div>
  62. </form>
  63. <a href="login.php">我有帳號,直接登錄</a>
  64. </div>
  65. </fieldset>
  66. <script>
  67. // 驗證二次密碼是否相等?
  68. function compare() {
  69. if (document.forms[0].p1.value.trim() !== document.forms[0].p2.value.trim()) {
  70. document.querySelector('#tips').innerText = '二次密碼不相等';
  71. return false;
  72. }
  73. }
  74. </script>
  75. </body>
  76. </html>

handle.php 會話處理控制器

  1. <?php
  2. /**
  3. * 會話處理控制器
  4. * 處理登錄、注冊、退出操作
  5. */
  6. namespace headerController;
  7. use PDO;
  8. // 開啟會話:必須寫在最前面
  9. session_start();
  10. // 查詢用書表中的數(shù)據user表
  11. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  12. $stmt = $db->prepare('SELECT * FROM `user`;');
  13. $stmt->execute();
  14. // 得到所有用戶數(shù)據
  15. $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
  16. //print_r($users);
  17. // 獲取用戶操作類型 login/register/logout
  18. // strtolower 轉小寫
  19. $action = strtolower($_GET['action']);
  20. // 根據類型進行不同的操作
  21. switch ($action) {
  22. // 1.登錄
  23. case 'login':
  24. // 檢查請求方式的類型
  25. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  26. // 獲取用戶請求的數(shù)據:郵箱和密碼
  27. $email = $_POST['email'];
  28. // sha1() 加密函數(shù) 返回長度為40的字符串
  29. $password = sha1($_POST['password']);
  30. // array_filter 過濾用戶數(shù)據,在數(shù)據表中查找$email和$password,有,返回結果集,沒有返回false
  31. $result = array_filter($users, function ($user) use ($email, $password) {
  32. return $user['email'] === $email && $user['password'] === $password;
  33. });
  34. // print_r($result);
  35. // die;
  36. // 判斷 $result 是否有數(shù)據
  37. if (count($result) > 1) {
  38. // 將用戶信息通過session保存serialize序列化后的數(shù)據到服務器上
  39. $_SESSION['user'] = serialize(array_slice($result, 0));
  40. exit('<script>alert("驗證通過");location.href="index.php"</script>');
  41. } else {
  42. // 登錄失敗 提示用戶未注冊
  43. exit("<script>alert('登錄失敗,郵箱: " . $email . "未注冊');location.href='login.php'</script>");
  44. }
  45. } else {
  46. exit('請求類型錯誤');
  47. }
  48. // 2.退出
  49. case 'logout':
  50. // 判斷session數(shù)據是否為空
  51. if (isset($_SESSION['user'])) {
  52. // 銷毀session 連文件一起刪除
  53. session_destroy();
  54. exit('<script>alert("退出成功");location.href="index.php"</script>');
  55. } else {
  56. exit('系統(tǒng)錯誤, session不存在');
  57. }
  58. // 3.注冊
  59. case 'register':
  60. // 獲取新用戶的數(shù)據
  61. $email = $_POST['email'];
  62. $name = $_POST['name'];
  63. $password = sha1($_POST['p2']);
  64. $register_data = time();
  65. // 檢查郵箱是否存在
  66. $result = array_filter($users, function ($user) use ($email) {
  67. return $user['email'] === $email;
  68. });
  69. print_r(count($result));
  70. // 判斷郵箱是否已注冊
  71. if (count($result) > 1) {
  72. exit("<script>alert('注冊失敗,郵箱: " . $email . "已注冊');location.href='register.php'</script>");
  73. } else {
  74. $sql = <<< SQL
  75. INSERT `user`
  76. SET `name`= ?,
  77. `email`= ?,
  78. `password`= ?,
  79. `register_data`= ?
  80. SQL;
  81. // 查詢新用戶信息
  82. $stmt = $db->prepare($sql);
  83. $data = [$name, $email, $password, $register_data];
  84. if ($stmt->execute($data)) {
  85. if ($stmt->rowCount() > 0) {
  86. // 注冊成功之后,讓用戶自動登錄
  87. $sql = 'SELECT * FROM `user` WHERE `id` = ' . $db->lastInsertId();
  88. $stmt = $db->prepare($sql);
  89. $stmt->execute();
  90. $newUser = $stmt->fetchAll(PDO::FETCH_ASSOC);
  91. // 序列化 保存到session
  92. $_SESSION['user'] = serialize($newUser);
  93. exit('<script>alert("注冊成功");location.href="index.php"</script>');
  94. } else {
  95. exit('<script>alert("注冊失敗");location.href="register.php"</script>');
  96. }
  97. } else {
  98. // 輸出sql執(zhí)行錯誤信息
  99. print_r($stmt->errorInfo());
  100. }
  101. }
  102. default:
  103. // 提示消息后結束執(zhí)行
  104. exit('參數(shù)非法或未定義操作');
  105. }

效果預覽

批改老師:PHPzPHPz

批改狀態(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+教程免費學