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

搜索
博主信息
博文 11
粉絲 0
評(píng)論 0
訪問量 30431
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
php會(huì)話流程控制cookie和session的應(yīng)用作業(yè)
飲雪煮茶
原創(chuàng)
884人瀏覽過

流程結(jié)構(gòu)

三個(gè)靜態(tài)頁

首頁效果圖

注冊(cè)頁面效果圖

登錄頁面效果圖

首頁index.php源碼

  1. <?php
  2. if (filter_has_var(INPUT_COOKIE, 'user')) {
  3. $user = unserialize(filter_input(INPUT_COOKIE, 'user'));
  4. }
  5. ?>
  6. <!DOCTYPE html>
  7. <html lang="zh-CN">
  8. <head>
  9. <meta charset="utf-8">
  10. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  11. <meta name="viewport" content="width=device-width, initial-scale=1">
  12. <!-- 上述3個(gè)meta標(biāo)簽*必須*放在最前面,任何其他內(nèi)容都*必須*跟隨其后! -->
  13. <title>網(wǎng)站首頁</title>
  14. <!-- Bootstrap -->
  15. <link rel="stylesheet" href="./css/index.css">
  16. </head>
  17. <body>
  18. <nav>
  19. <div class="container">
  20. <a href="">我的網(wǎng)站</a>
  21. <span>
  22. <?php if (isset($user)) : ?>
  23. <span><?php echo $user['user']; ?></span>
  24. <a href="handle.php?action=logout">退出</a>
  25. <?php else : ?>
  26. <a href="login.php">登錄</a>
  27. <?php endif; ?>
  28. </span>
  29. </div>
  30. </nav>
  31. </body>
  32. </html>

登錄頁面 login.php源碼

  1. <?php
  2. if (filter_has_var(INPUT_COOKIE, 'user')) {
  3. exit('<script> alert("已經(jīng)登錄請(qǐng)不要重復(fù)登錄!"); location.href = "index.php";</script>');
  4. }
  5. ?>
  6. <!DOCTYPE html>
  7. <html lang="zh-CN">
  8. <head>
  9. <meta charset="utf-8">
  10. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  11. <meta name="viewport" content="width=device-width, initial-scale=1">
  12. <!-- 上述3個(gè)meta標(biāo)簽*必須*放在最前面,任何其他內(nèi)容都*必須*跟隨其后! -->
  13. <title>用戶登錄</title>
  14. <!-- Bootstrap -->
  15. <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
  16. <link rel="stylesheet" href="./css/style.css">
  17. </head>
  18. <body>
  19. <form class="form-horizontal" method="POST" action="handle.php?action=login">
  20. <div class="col-sm-12 title">
  21. <h3>用戶登錄</h3>
  22. </div>
  23. <div class="form-group">
  24. <label for="email" class="col-sm-4 control-label">郵&nbsp;&nbsp;&nbsp;箱:</label>
  25. <div class="col-sm-8">
  26. <input type="email" name="email" class="form-control" id="email" placeholder="請(qǐng)輸入郵箱">
  27. </div>
  28. </div>
  29. <div class="form-group">
  30. <label for="password" class="col-sm-4 control-label">密&nbsp;&nbsp;&nbsp;碼:</label>
  31. <div class="col-sm-8">
  32. <input type="password" name="password" class="form-control" id="password" placeholder="請(qǐng)輸入密碼">
  33. </div>
  34. </div>
  35. <div class="form-group">
  36. </div>
  37. <div class="form-group">
  38. <div class="col-sm-12">
  39. <button type="submit" class="btn btn-info btn-block">登錄</button>
  40. </div>
  41. </div>
  42. <span><a href="register.php">還沒有賬號(hào),注冊(cè)一個(gè)。</a></span>
  43. </form>
  44. </body>
  45. </html>

注冊(cè)頁面register.php源碼

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <!-- 上述3個(gè)meta標(biāo)簽*必須*放在最前面,任何其他內(nèi)容都*必須*跟隨其后! -->
  8. <title>注冊(cè)新用戶</title>
  9. <!-- Bootstrap -->
  10. <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
  11. <link rel="stylesheet" href="./css/style.css">
  12. </head>
  13. <body>
  14. <form class="form-horizontal" method="POST" action="handle.php?action=register">
  15. <div class="col-sm-12 title">
  16. <h3>注冊(cè)新用戶</h3>
  17. </div>
  18. <div class="form-group">
  19. <label for="user" class="col-sm-4 control-label">用戶名:</label>
  20. <div class="col-sm-8">
  21. <input type="text" name="user" class="form-control" id="user" placeholder="Email">
  22. </div>
  23. </div>
  24. <div class="form-group">
  25. <label for="email" class="col-sm-4 control-label">郵&nbsp;&nbsp;&nbsp;箱:</label>
  26. <div class="col-sm-8">
  27. <input type="text" name="email" class="form-control" id="email" placeholder="Email">
  28. </div>
  29. </div>
  30. <div class="form-group">
  31. <label for="password" class="col-sm-4 control-label">密&nbsp;&nbsp;&nbsp;碼:</label>
  32. <div class="col-sm-8">
  33. <input type="password" name="password" class="form-control" id="password" placeholder="Password">
  34. </div>
  35. </div>
  36. <div class="form-group">
  37. <label for="repassword" class="col-sm-4 control-label">重復(fù)密碼:</label>
  38. <div class="col-sm-8">
  39. <input type="password" class="form-control" id="repassword" placeholder="Email">
  40. </div>
  41. </div>
  42. <div class="form-group">
  43. </div>
  44. <div class="form-group">
  45. <div class="col-sm-12">
  46. <button type="submit" class="btn btn-info btn-block">登錄</button>
  47. </div>
  48. </div>
  49. <span><a href="login.php">已經(jīng)有帳號(hào)了,直接登錄。</a></span>
  50. </form>
  51. </body>
  52. </html>

控制頁面 haddle.php源碼

  1. <?php
  2. //用戶數(shù)據(jù)組
  3. $users = [
  4. ['id' => 1, 'user' => 'admin', 'email' => 'admin@qq.com', 'password' => '7c4a8d09ca3762af61e59520943dc26494f8941b'],
  5. ['id' => 2, 'user' => 'php', 'email' => 'php@qq.com', 'password' => '7c4a8d09ca3762af61e59520943dc26494f8941b']
  6. ];
  7. //來源頁面判斷
  8. //來源頁面白名單組
  9. $allowUrl = ['index.php', 'login.php', 'register.php'];
  10. //獲取當(dāng)前訪問來源地址文件名
  11. $currentUrl = basename(filter_input(INPUT_SERVER, 'HTTP_REFERER'));
  12. //判斷是否再白名單內(nèi)
  13. if (!in_array($currentUrl, $allowUrl)) {
  14. exit('非法請(qǐng)求地址');
  15. }
  16. //獲取當(dāng)前url訪問action參數(shù)值
  17. $action = filter_input(INPUT_GET, 'action', FILTER_SANITIZE_STRING);
  18. //抓換成小寫
  19. $action = strtolower($action);
  20. switch ($action) {
  21. case 'login':
  22. if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') === 'POST') {
  23. $email = filter_var(filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL), FILTER_SANITIZE_EMAIL);
  24. $password = sha1(filter_input(INPUT_POST, 'password'));
  25. // echo $email,$password;
  26. $result = array_filter($users, function ($user) use ($email, $password) {
  27. return $email === $user['email'] && $password === $user['password'];
  28. });
  29. // print_r($result);die;
  30. if (count($result) === 1) {
  31. setcookie('user', serialize(array_pop($result)));
  32. exit('<script> alert("登錄成功"); location.href = "index.php";</script>');
  33. } else {
  34. exit('<script> alert("登錄失敗,用戶名或者密碼錯(cuò)誤,請(qǐng)重新登錄"); location.href = "login.php";</script>');
  35. }
  36. } else {
  37. exit('非法請(qǐng)求!');
  38. }
  39. break;
  40. case 'register':
  41. if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') === 'POST') {
  42. $id = 3;
  43. $user = filter_input(INPUT_POST, 'user');
  44. $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
  45. $password = sha1(filter_input(INPUT_POST, 'password'));
  46. // echo $id,$user,$email,$password;
  47. $data = compact('id', 'user', 'email', 'password');
  48. array_push($users, $data);
  49. print_r($users);
  50. exit('<script> alert("用戶名注冊(cè)成功"); location.href = "login.php";</script>');
  51. }
  52. break;
  53. case 'logout':
  54. if (filter_input(INPUT_COOKIE, 'user')) {
  55. setcookie('user', null, time() - 3600);
  56. exit('<script> alert("退出登錄");location.href = "index.php";</script>');
  57. }
  58. break;
  59. default:
  60. exit('未定義變量');
  61. }

兩個(gè)css源碼

index.css

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. nav {
  6. background-color: #424242;
  7. height: 50px;
  8. }
  9. nav>.container {
  10. max-width: 1200px;
  11. margin: auto;
  12. height: 50px;
  13. font-size: 16px;
  14. display: flex;
  15. justify-content: space-between;
  16. align-items: center;
  17. }
  18. a {
  19. color: #fff;
  20. text-decoration: none;
  21. }

style.css

  1. body {
  2. display: flex;
  3. justify-content: center;
  4. margin-top: 150px;
  5. background-color: #eee;
  6. }
  7. form {
  8. width: 400px;
  9. border: 1px solid #ddd;
  10. padding: 20px 50px;
  11. background-color: #fff;
  12. border-radius: 3px;
  13. box-shadow: 1px 1px 2px #888;
  14. }
  15. .title {
  16. display: flex;
  17. justify-content: center;
  18. margin-bottom: 20px;
  19. }

session實(shí)現(xiàn)會(huì)話控制

index.php源碼

  1. <?php
  2. session_start();
  3. if(isset($_SESSION['user'])){
  4. $user = $_SESSION['user'];
  5. }
  6. ?>
  7. <!DOCTYPE html>
  8. <html lang="zh-CN">
  9. <head>
  10. <meta charset="utf-8">
  11. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  12. <meta name="viewport" content="width=device-width, initial-scale=1">
  13. <!-- 上述3個(gè)meta標(biāo)簽*必須*放在最前面,任何其他內(nèi)容都*必須*跟隨其后! -->
  14. <title>網(wǎng)站首頁</title>
  15. <!-- Bootstrap -->
  16. <link rel="stylesheet" href="./css/index.css">
  17. </head>
  18. <body>
  19. <nav>
  20. <div class="container">
  21. <a href="">我的網(wǎng)站</a>
  22. <span>
  23. <?php if (isset($user)) : ?>
  24. <span><?php echo $user['user']; ?></span>
  25. <a href="handle.php?action=logout">退出</a>
  26. <?php else : ?>
  27. <a href="login.php">登錄</a>
  28. <?php endif; ?>
  29. </span>
  30. </div>
  31. </nav>
  32. </body>
  33. </html>

login.php

  1. <?php
  2. session_start();
  3. if(isset($_SESSION['user'])){
  4. exit('<script> alert("已經(jīng)登錄請(qǐng)不要重復(fù)登錄!"); location.href = "index.php";</script>');
  5. }
  6. ?>
  7. <!DOCTYPE html>
  8. <html lang="zh-CN">
  9. <head>
  10. <meta charset="utf-8">
  11. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  12. <meta name="viewport" content="width=device-width, initial-scale=1">
  13. <!-- 上述3個(gè)meta標(biāo)簽*必須*放在最前面,任何其他內(nèi)容都*必須*跟隨其后! -->
  14. <title>用戶登錄</title>
  15. <!-- Bootstrap -->
  16. <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
  17. <link rel="stylesheet" href="./css/style.css">
  18. </head>
  19. <body>
  20. <form class="form-horizontal" method="POST" action="handle.php?action=login">
  21. <div class="col-sm-12 title">
  22. <h3>用戶登錄</h3>
  23. </div>
  24. <div class="form-group">
  25. <label for="email" class="col-sm-4 control-label">郵&nbsp;&nbsp;&nbsp;箱:</label>
  26. <div class="col-sm-8">
  27. <input type="email" name="email" class="form-control" id="email" placeholder="請(qǐng)輸入郵箱">
  28. </div>
  29. </div>
  30. <div class="form-group">
  31. <label for="password" class="col-sm-4 control-label">密&nbsp;&nbsp;&nbsp;碼:</label>
  32. <div class="col-sm-8">
  33. <input type="password" name="password" class="form-control" id="password" placeholder="請(qǐng)輸入密碼">
  34. </div>
  35. </div>
  36. <div class="form-group">
  37. </div>
  38. <div class="form-group">
  39. <div class="col-sm-12">
  40. <button type="submit" class="btn btn-info btn-block">登錄</button>
  41. </div>
  42. </div>
  43. <span><a href="register.php">還沒有賬號(hào),注冊(cè)一個(gè)。</a></span>
  44. </form>
  45. </body>
  46. </html>

控制頁面handle.php源碼

  1. <?php
  2. session_start();
  3. //用戶數(shù)據(jù)組
  4. $users = [
  5. ['id' => 1, 'user' => 'admin', 'email' => 'admin@qq.com', 'password' => '7c4a8d09ca3762af61e59520943dc26494f8941b'],
  6. ['id' => 2, 'user' => 'php', 'email' => 'php@qq.com', 'password' => '7c4a8d09ca3762af61e59520943dc26494f8941b']
  7. ];
  8. //來源頁面判斷
  9. //來源頁面白名單組
  10. $allowUrl = ['index.php', 'login.php', 'register.php'];
  11. //獲取當(dāng)前訪問來源地址文件名
  12. $currentUrl = basename(filter_input(INPUT_SERVER, 'HTTP_REFERER'));
  13. //判斷是否再白名單內(nèi)
  14. if (!in_array($currentUrl, $allowUrl)) {
  15. exit('非法請(qǐng)求地址');
  16. }
  17. //獲取當(dāng)前url訪問action參數(shù)值
  18. $action = filter_input(INPUT_GET, 'action', FILTER_SANITIZE_STRING);
  19. //抓換成小寫
  20. $action = strtolower($action);
  21. switch ($action) {
  22. case 'login':
  23. if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') === 'POST') {
  24. $email = filter_var(filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL), FILTER_SANITIZE_EMAIL);
  25. $password = sha1(filter_input(INPUT_POST, 'password'));
  26. // echo $email,$password;
  27. $result = array_filter($users, function ($user) use ($email, $password) {
  28. return $email === $user['email'] && $password === $user['password'];
  29. });
  30. // print_r($result);die;
  31. if (count($result) === 1) {
  32. // setcookie('user', serialize(array_pop($result)));
  33. $_SESSION['user'] = array_pop($result);
  34. exit('<script> alert("登錄成功"); location.href = "index.php";</script>');
  35. } else {
  36. exit('<script> alert("登錄失敗,用戶名或者密碼錯(cuò)誤,請(qǐng)重新登錄"); location.href = "login.php";</script>');
  37. }
  38. } else {
  39. exit('非法請(qǐng)求!');
  40. }
  41. break;
  42. case 'register':
  43. if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') === 'POST') {
  44. $id = 3;
  45. $user = filter_input(INPUT_POST, 'user');
  46. $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
  47. $password = sha1(filter_input(INPUT_POST, 'password'));
  48. // echo $id,$user,$email,$password;
  49. $data = compact('id', 'user', 'email', 'password');
  50. array_push($users, $data);
  51. print_r($users);
  52. exit('<script> alert("用戶名注冊(cè)成功"); location.href = "login.php";</script>');
  53. }
  54. break;
  55. case 'logout':
  56. if (isset($_SESSION['user'])) {
  57. // setcookie('user', null, time() - 3600);
  58. session_destroy();
  59. exit('<script> alert("退出登錄");location.href = "index.php";</script>');
  60. }
  61. break;
  62. default:
  63. exit('未定義變量');
  64. }

其他頁面與cookie相同。

批改狀態(tài):未批改

老師批語:
本博文版權(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é)議
0條評(píng)論
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
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é)