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

搜索
博主信息
博文 33
粉絲 0
評論 0
訪問量 34417
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
服務(wù)端 - PHP - 會話控制之Session實現(xiàn)
原創(chuàng)
796人瀏覽過

服務(wù)端 - PHP - 會話控制之Session實現(xiàn)

一、目錄結(jié)構(gòu)

  1. ├─WWW 應(yīng)用目錄
  2. │ ├─user 用戶模塊
  3. │ │ ├─login.php 登錄頁面
  4. │ │ └─register.php 注冊頁面
  5. │ │
  6. │ ├─css 樣式目錄
  7. │ │ ├─index.css 首頁樣式
  8. │ │ ├─login.css 登錄頁面樣式
  9. │ │ └─register.css 注冊頁面樣式
  10. │ │
  11. │ ├─index.php 首頁
  12. │ ├─handle.php 控制器

二、實現(xiàn)

1. index.php & index.css

  1. <?php
  2. //開啟會話
  3. session_start();
  4. //判斷是否登錄
  5. if (isset($_SESSION['user'])) {
  6. //如果已登錄則取出SESSIONID
  7. $un = $_SESSION['user'];
  8. }
  9. ?>
  10. <!DOCTYPE html>
  11. <html lang="zh_hans">
  12. <head>
  13. <meta charset="UTF-8">
  14. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  15. <title>首頁</title>
  16. <link rel="stylesheet" href="css\index.css">
  17. </head>
  18. <body>
  19. <nav>
  20. <span class="l-nav">
  21. <ul>
  22. <li><a href="#">首頁</a></li>
  23. <li><a href="#">文章</a></li>
  24. <li><a href="#">資料</a></li>
  25. <li><a href="#">關(guān)于</a></li>
  26. </ul>
  27. </span>
  28. <span class="r-nav">
  29. <?php
  30. //如果已登錄的話顯示用戶名和退出按鈕
  31. if (isset($un)):
  32. ?>
  33. <span style="color: yellow;font-size: larger;font-weight: bolder;line-height: 40px;"><?php echo $un?></span>
  34. <a href="" id="logout">
  35. 退出
  36. </a>
  37. <?php
  38. //否則顯示登錄按鈕
  39. else:
  40. ?>
  41. <a href="\user\login.php" id="login">登錄</a>
  42. <?php endif ?>
  43. </span>
  44. </nav>
  45. <script>
  46. //為退出按鈕創(chuàng)建一個事件,點(diǎn)擊它會讓其跳轉(zhuǎn)到退出事件處理器
  47. document.querySelector('#logout').addEventListener('click', function(event) {
  48. //彈出對話框
  49. if (confirm('是否退出')) {
  50. //取消默認(rèn)行為, 其實就是禁用原<a>標(biāo)簽的點(diǎn)擊跳轉(zhuǎn)行為,使用事件中的自定義方法處理
  51. event.preventDefault();
  52. //跳轉(zhuǎn)到退出事件處理器
  53. window.location.assign('handle.php?action=logout');
  54. }
  55. });
  56. </script>
  57. </body>
  58. </html>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. a {
  7. color: white;
  8. font-size: larger;
  9. font-weight: bolder;
  10. text-decoration: none;
  11. }
  12. nav {
  13. width: 100%;
  14. height: 40px;
  15. background-color: black;
  16. display: flex;
  17. justify-content: space-between;
  18. align-items: center;
  19. }
  20. .l-nav > ul {
  21. display: flex;
  22. list-style: none;
  23. }
  24. .l-nav > ul > li {
  25. width: 100px;
  26. height: 40px;
  27. }
  28. .l-nav > ul > li > a {
  29. width: 100%;
  30. height: 100%;
  31. margin-left: 20px;
  32. display: flex;
  33. justify-content: center;
  34. align-items: center;
  35. }
  36. .l-nav > ul > li > a:hover {
  37. color: #303;
  38. background-color: yellow;
  39. }
  40. .r-nav {
  41. display: flex;
  42. justify-content: space-between;
  43. width: 150px;
  44. height: 40px;
  45. margin-right: 20px;
  46. }
  47. .r-nav > a {
  48. width: 100px;
  49. height: 100%;
  50. display: flex;
  51. justify-content: center;
  52. align-items: center;
  53. }
  54. .r-nav > a:hover {
  55. color: #303;
  56. background-color: yellow;
  57. }



2. login.php & login.css

  1. <?php
  2. //開啟會話
  3. session_start();
  4. //判斷是否登錄
  5. if (isset($_SESSION['user'])) {
  6. //如果已登錄則輸出請勿重復(fù)登錄的信息并返回首頁
  7. exit('<script>alert("請勿重復(fù)登錄");location.href="/index.php"</script>');
  8. }
  9. ?>
  10. <!DOCTYPE html>
  11. <html lang="zh_hans">
  12. <head>
  13. <meta charset="UTF-8">
  14. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  15. <title>登錄頁</title>
  16. <link rel="stylesheet" href="\css\login.css">
  17. </head>
  18. <body>
  19. <h2>登錄表單</h2>
  20. <div class="f-box">
  21. <form action="..\handle.php?action=login" method="POST">
  22. <fieldset>
  23. <div class="e-box">
  24. <label for="email">郵箱:</label>
  25. <input type="email" name="email" id="email" require autofocus autocomplete="on">
  26. </div>
  27. <div class="p-box">
  28. <label for="password">密碼:</label>
  29. <input type="password" name="password" id="password" require>
  30. </div>
  31. <div class="s-box">
  32. <button type="submit">提交</button>
  33. </div>
  34. <div class="o-box">
  35. <!--鏈接到注冊頁-->
  36. <a href="register.php">還沒有賬戶?注冊一個吧!</a>
  37. </div>
  38. </fieldset>
  39. </form>
  40. </div>
  41. </body>
  42. </html>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. a {
  7. color: black;
  8. text-decoration: none;
  9. }
  10. h2 {
  11. width: 300px;
  12. height: 40px;
  13. margin: 10px auto 0 auto;
  14. text-align: center;
  15. line-height: 40px;
  16. }
  17. .f-box {
  18. width: 400px;
  19. height: 500px;
  20. margin: 20px auto;
  21. }
  22. .e-box {
  23. width: 300px;
  24. height: 30px;
  25. margin: 25px auto 0 auto;
  26. }
  27. .e-box input {
  28. width: 240px;
  29. height: 100%;
  30. }
  31. .p-box {
  32. width: 300px;
  33. height: 30px;
  34. margin: 10px auto;
  35. }
  36. .p-box input {
  37. width: 240px;
  38. height: 100%;
  39. }
  40. .s-box {
  41. width: 300px;
  42. height: 30px;
  43. margin: 0 auto;
  44. }
  45. .s-box button {
  46. width: 100%;
  47. height: 100%;
  48. }
  49. .s-box button:hover {
  50. background-color: lightyellow;
  51. }
  52. .o-box {
  53. width: 200px;
  54. margin: 10px auto 30px auto;
  55. }

3. register.php & register.css

  1. <?php
  2. //開啟會話
  3. session_start();
  4. //判斷是否登錄
  5. if (isset($_SESSION['user'])) {
  6. //如果已登錄則輸出請勿重復(fù)登錄的信息并返回首頁
  7. exit('<script>alert("請勿重復(fù)登錄");location.href="/index.php"</script>');
  8. }
  9. ?>
  10. <!DOCTYPE html>
  11. <html lang="zh_hans">
  12. <head>
  13. <meta charset="UTF-8">
  14. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  15. <title>注冊頁</title>
  16. <link rel="stylesheet" href="\css\register.css">
  17. </head>
  18. <body>
  19. <h2>注冊表單</h2>
  20. <div class="f-box">
  21. <!--給一個提交驗證,如果驗證失敗中止提交-->
  22. <form action="..\handle.php?action=register" method="POST" onsubmit="return compare()">
  23. <fieldset>
  24. <div class="d-box">
  25. <label for="username">姓名:</label>
  26. <input type="text" name="username" id="username" required autofocus autocomplete="on">
  27. </div>
  28. <div class="d-box">
  29. <label for="email">郵箱:</label>
  30. <input type="email" name="email" id="email" required autocomplete="on">
  31. </div>
  32. <div class="d-box">
  33. <label for="p1">密碼:</label>
  34. <input type="password" name="p1" id="p1" required>
  35. </div>
  36. <div class="d-box">
  37. <label for="p2">重復(fù):</label>
  38. <input type="password" name="p2" id="p2" required>
  39. </div>
  40. <div class="d-box">
  41. <button type="submit">提交</button>
  42. </div>
  43. <div class="d-box">
  44. <a href="login.php">已有賬號?直接登錄!</a>
  45. </div>
  46. </fieldset>
  47. </form>
  48. </div>
  49. <script>
  50. //驗證二次密碼是否相等
  51. function compare() {
  52. if (document.forms[0].p1.value.trim() !== document.forms[0].p2.value.trim()) {
  53. window.alert('二次密碼不相等');
  54. return false;
  55. };
  56. }
  57. </script>
  58. </body>
  59. </html>
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. a {
  7. color: black;
  8. text-decoration: none;
  9. }
  10. h2 {
  11. width: 300px;
  12. height: 40px;
  13. margin: 10px auto 0 auto;
  14. text-align: center;
  15. line-height: 40px;
  16. }
  17. .f-box {
  18. width: 400px;
  19. height: 600px;
  20. margin: 20px auto;
  21. }
  22. .d-box:nth-of-type(1) {
  23. width: 300px;
  24. height: 30px;
  25. margin: 25px auto 0 auto;
  26. }
  27. .d-box:nth-of-type(1) input {
  28. width: 240px;
  29. height: 100%;
  30. }
  31. .d-box:nth-of-type(2) {
  32. width: 300px;
  33. height: 30px;
  34. margin: 10px auto;
  35. }
  36. .d-box:nth-of-type(2) input {
  37. width: 240px;
  38. height: 100%;
  39. }
  40. .d-box:nth-of-type(3) {
  41. width: 300px;
  42. height: 30px;
  43. margin: 10px auto;
  44. }
  45. .d-box:nth-of-type(3) input {
  46. width: 240px;
  47. height: 100%;
  48. }
  49. .d-box:nth-of-type(4) {
  50. width: 300px;
  51. height: 30px;
  52. margin: 10px auto;
  53. }
  54. .d-box:nth-of-type(4) input {
  55. width: 240px;
  56. height: 100%;
  57. }
  58. .d-box:nth-of-type(5) {
  59. width: 300px;
  60. height: 30px;
  61. margin: 0 auto;
  62. }
  63. .d-box:nth-of-type(5) button {
  64. width: 100%;
  65. height: 100%;
  66. }
  67. .d-box:nth-of-type(5) button:hover {
  68. background-color: lightyellow;
  69. }
  70. .d-box:nth-of-type(6) {
  71. width: 200px;
  72. margin: 10px auto 30px auto;
  73. }

4. handle.php

  1. <?php
  2. //開啟會話
  3. session_start();
  4. //1. 獲取表中用戶名、郵箱和密碼字段數(shù)據(jù)
  5. $pdo = new PDO('mysql:host=localhost;dbname=shopping', 'root', 'root');
  6. $stmt = $pdo->prepare('SELECT `user_name`, `email`, `passwd` FROM `userinfo`');
  7. $stmt->execute();
  8. $t_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
  9. //2. 處理用戶登錄、退出與注冊
  10. //獲取請求參數(shù)值并根據(jù)其來使用對應(yīng)的方法進(jìn)行處理
  11. $action = $_GET['action'];
  12. switch (strtolower($action)) {
  13. //2.1 登錄
  14. case 'login':
  15. //判斷請求是否合法
  16. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  17. //獲取從前端傳過來的數(shù)據(jù)
  18. $f_email = $_POST['email'];
  19. $f_passwd = md5($_POST['password']);
  20. //與后臺數(shù)據(jù)進(jìn)行校驗并獲取
  21. $results = array_filter($t_data, function($user) use ($f_email, $f_passwd) {
  22. return $user['email'] === $f_email && $user['passwd'] === $f_passwd;
  23. });
  24. //如果有記錄證明校驗成功并在服務(wù)端設(shè)置SESSIONID
  25. if (count($results) === 1) {
  26. $user_v = (current($results))['user_name'];
  27. $_SESSION['user'] = $user_v;
  28. //校驗通過并設(shè)置好SESSIONID后退出當(dāng)前頁面返回首頁
  29. exit('<script>alert("驗證通過");location.href="index.php"</script>');
  30. } else {
  31. //校驗不通過輸出錯誤信息后返回當(dāng)前頁面
  32. exit('<script>alert("郵箱或密碼錯誤,或者還未注冊賬戶");location.href="user/login.php"</script>');
  33. }
  34. } else {
  35. die('請求類型錯誤');
  36. }
  37. break;
  38. //2.2 退出
  39. case 'logout':
  40. //判斷是否登錄,已登錄則清除SESSIONID并返回首頁
  41. if (isset($_SESSION['user'])) {
  42. session_destroy();
  43. exit('<script>alert("退出成功");location.href="/index.php"</script>');
  44. }
  45. break;
  46. //2.3 注冊
  47. case 'register':
  48. //獲取從前端傳過來的數(shù)據(jù)
  49. $username = $_POST['username'];
  50. $email = $_POST['email'];
  51. $pwd = md5($_POST['p1']);
  52. //定義非空字段數(shù)據(jù)
  53. $reg_time = time();
  54. //將新用戶數(shù)據(jù)插入到表中
  55. $sql = "INSERT `userinfo` SET `user_name`='{$username}', `passwd`='{$pwd}', `email`='{$email}', `reg_time`='{$reg_time}'";
  56. $stmt = $pdo->prepare($sql);
  57. $stmt->execute();
  58. //判斷是否插入成功
  59. if ($stmt->rowCount() === 1) {
  60. //插入成功代表注冊成功并返回至登錄頁面
  61. exit('<script>alert("注冊成功");location.href="user/login.php"</script>');
  62. } else {
  63. //注冊失敗輸出錯誤信息后返回當(dāng)前頁面
  64. exit('<script>alert("注冊失敗");location.href="user/register.php"</script>');
  65. }
  66. break;
  67. //未定義
  68. default:
  69. exit('未定義操作');
  70. }

三、課程總結(jié)

  • 今天學(xué)習(xí)了 PHP 的會話控制,通過上課認(rèn)真聽講和認(rèn)真完成老師布置的作業(yè),使得我對 PHP 會話控制的理解和運(yùn)用更加深入和熟悉。最主要的知識點(diǎn)是明白和掌握了Session的特點(diǎn)以及Session方式實現(xiàn)會話控制的基本用法。
批改老師:天蓬老師天蓬老師

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

老師批語:完成的相當(dāng)出色
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報處理!
全部評論 文明上網(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
隨時隨地碎片化學(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é)