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

搜索
博主信息
博文 145
粉絲 7
評(píng)論 7
訪問(wèn)量 198879
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
01月14日作業(yè):登陸注冊(cè)驗(yàn)證案例作業(yè)(cookie和session)
李東亞1??3????12?
原創(chuàng)
1186人瀏覽過(guò)

作業(yè)一 cookie版

1、代碼:

1、首頁(yè)代碼

  1. //判斷登陸狀態(tài),輸出顯示登陸狀態(tài)變量(沒(méi)有學(xué)JS利用PHP的if語(yǔ)句來(lái)判斷輸出登陸和登出狀態(tài))
  2. if (filter_has_var(INPUT_COOKIE,'user')) {
  3. $user=unserialize(filter_input(INPUT_COOKIE,'user'));
  4. $users=$user['username'];
  5. $status='退出';
  6. $status_url='0114verify.php?action=logout';
  7. }else {
  8. $users='用戶注冊(cè)';
  9. $user_url='0114register.php';
  10. $status='登陸';
  11. $status_url='0114login.php';
  12. }
  13. ?>
  14. <!doctype html>
  15. <html lang="en">
  16. <head>
  17. <meta charset="UTF-8">
  18. <title>種業(yè)圈</title>
  19. <style>
  20. * {
  21. margin:0;
  22. padding: 0;
  23. font-size: 13px;
  24. color:#666666;
  25. }
  26. a {
  27. text-decoration: none;
  28. }
  29. .header {
  30. width: 100%;
  31. height: 100px;
  32. background-color: #333333;
  33. display: flex;
  34. flex-flow: row nowrap;
  35. justify-content: space-between;
  36. align-items: center;
  37. padding: 0 20px;
  38. box-sizing: border-box;
  39. }
  40. .header > .right {
  41. width: 120px;
  42. align-self: flex-end;
  43. margin-bottom: 20px;
  44. margin-right: 40px;
  45. display: flex;
  46. justify-content: space-between;
  47. }
  48. .header > .right * {
  49. font-size: 18px;
  50. color:#fffdef;
  51. }
  52. .header > .right a:hover {
  53. color:#ff0000;
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <div class="header">
  59. <a href="0114index.php">
  60. <img src="../0110/images/logo.png" alt="">
  61. </a>
  62. <div class="right">
  63. //根據(jù)判斷輸出的變量信息顯示登陸狀態(tài)
  64. <span><a href="<?php echo $user_url; ?>"><?php echo $users; ?></a></span>
  65. <a id='logout' href="<?php echo $status_url; ?>"><?php echo $status; ?></a>
  66. </div>
  67. </div>
  68. </body>
  69. </html>

2、登陸頁(yè)面代碼:

  1. if (filter_has_var(INPUT_COOKIE,'user')) {
  2. exit('<script>alert("請(qǐng)勿重復(fù)登陸");location.assign("0114index.php");</script>');
  3. }
  4. ?>
  5. <!doctype html>
  6. <html lang="en">
  7. <head>
  8. <meta charset="UTF-8">
  9. <title>登陸</title>
  10. <style>
  11. a {
  12. text-decoration: none;
  13. }
  14. .login{
  15. width: 400px;
  16. height: 250px;
  17. background-color: #55a532;
  18. margin:0 auto;
  19. padding: 10px;
  20. font-size: 16px;
  21. color: #fffdef;
  22. border-radius: 20px;
  23. }
  24. .login:hover {
  25. box-shadow: 0 0 3px #333333 ;
  26. }
  27. .login > h3 {
  28. width: 80px;
  29. margin: 20px auto;
  30. color: #222222;
  31. }
  32. .login > :last-child {
  33. width: 300px;
  34. margin: 10px auto;
  35. text-align: center;
  36. }
  37. .login > form {
  38. display: grid;
  39. grid-template-columns: 70px 1fr;
  40. grid-template-rows:repeat(3,40px) ;
  41. place-items: center;
  42. }
  43. .login > form > input {
  44. width: 200px;
  45. height: 25px;
  46. }
  47. .login > form > input:nth-last-of-type(n+3):hover {
  48. box-shadow: 0 0 3px #df5000 inset;
  49. }
  50. .login > form > label {
  51. width: 80px;
  52. height: 25px;
  53. position: relative;
  54. left:60px;
  55. }
  56. .login > form > input:nth-last-of-type(-n+2) {
  57. margin: 15px 0;
  58. width:100px;
  59. height:35px;
  60. border-style: none;
  61. background-color: #ff0000;
  62. color:#fffdef;
  63. position: relative;
  64. left:66px;
  65. }
  66. .login > form > input:nth-last-of-type(-n+2):hover{
  67. background-color: #178CEE;
  68. }
  69. </style>
  70. </head>
  71. <body>
  72. <div class="login">
  73. <h3>用戶登陸</h3>
  74. <form action="0114verify.php?action=login" method="post" name="sign-in">
  75. <label for="username"><span>用戶名:</span></label>
  76. <input type="text" name="username" id="username" autofocus placeholder="請(qǐng)輸入你的賬號(hào)" required>
  77. <label for="pwd1"><span>密 碼:</span></label>
  78. <input type="password" name="pwd" id="pwd" placeholder="請(qǐng)輸入你的密碼" required>
  79. <input type="submit" value="登陸">
  80. <input type="reset" value="重置">
  81. </form>
  82. <div>
  83. <span>沒(méi)有賬號(hào),請(qǐng)點(diǎn)此處 <a href="0114register.php">注冊(cè)</a> </span>
  84. </div>
  85. </div>
  86. </body>
  87. </html>

3、注冊(cè)頁(yè)面代碼(利用之前做的頁(yè)面沒(méi)有做修改,但只利用用戶名和密碼1、密碼2三項(xiàng),其他項(xiàng)不考慮):

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>登陸</title>
  6. <style>
  7. a {
  8. text-decoration: none;
  9. }
  10. .login{
  11. width: 400px;
  12. height: 450px;
  13. background-color: #55a532;
  14. margin:0 auto;
  15. padding: 10px;
  16. font-size: 16px;
  17. color: #fffdef;
  18. border-radius: 20px;
  19. }
  20. .login:hover {
  21. box-shadow: 0 0 3px #333333 ;
  22. }
  23. .login > h3 {
  24. width: 100px;
  25. margin: 20px auto;
  26. color: #222222;
  27. }
  28. .login > :last-child {
  29. width: 300px;
  30. margin: 10px auto;
  31. text-align: center;
  32. }
  33. .login > form {
  34. display: grid;
  35. grid-template-columns: 70px 1fr;
  36. grid-template-rows:repeat(7,40px) ;
  37. place-items: center;
  38. }
  39. .login > form > input {
  40. width: 200px;
  41. height: 25px;
  42. }
  43. .login > form > input:nth-last-of-type(n+3):hover {
  44. box-shadow: 0 0 3px #df5000 inset;
  45. }
  46. .login > form > label {
  47. width: 80px;
  48. height: 25px;
  49. position: relative;
  50. left:60px;
  51. }
  52. .login > form > input:nth-last-of-type(-n+2) {
  53. margin: 15px 0;
  54. width:100px;
  55. height:35px;
  56. border-style: none;
  57. background-color: #ff0000;
  58. color:#fffdef;
  59. position: relative;
  60. left:66px;
  61. }
  62. .login > form > input:nth-last-of-type(-n+2):hover{
  63. background-color: #178CEE;
  64. }
  65. </style>
  66. </head>
  67. <body>
  68. <div class="login">
  69. <h3>新用戶注冊(cè)</h3>
  70. <form action="0114verify.php?action=register" method="post" name="sign-in">
  71. <label for="username"><span>用戶名:</span></label>
  72. <input type="text" name="username" id="username" autofocus placeholder="請(qǐng)輸入你的賬號(hào)" required>
  73. <label for="pwd1"><span>密 碼:</span></label>
  74. <input type="password" name="pwd1" id="pwd1" placeholder="請(qǐng)輸入你的密碼" required>
  75. <label for="pwd2"><span>密 碼:</span></label>
  76. <input type="password" name="pwd2" id="pwd2" placeholder="請(qǐng)?jiān)俅屋斎肽愕拿艽a" required>
  77. <label for="email"><span>郵箱</span></label>
  78. <input type="email" name="email" id="email" placeholder="請(qǐng)輸入郵箱" required>
  79. <label for="tel"><span>電話</span></label>
  80. <input type="tel" name="tel" id="tel" placeholder="請(qǐng)輸入手機(jī)號(hào)" required>
  81. <label for="gender"><span>性別</span></label>
  82. <div>
  83. <input type="radio" name="gender" value="1" checked>
  84. <input type="radio" name="gender" value="2" >
  85. <input type="radio" name="gender" value="3" >保密
  86. </div>
  87. <label for="like"><span>興趣愛(ài)好</span></label>
  88. <div>
  89. <input type="checkbox" name="like[]" value="1">html
  90. <input type="checkbox" name="like[]" value="2">css
  91. <input type="checkbox" name="like[]" value="3" checked id="like">php
  92. </div>
  93. <input type="submit" value="注冊(cè)">
  94. <input type="reset" value="重置">
  95. </form>
  96. <div>
  97. <span>已有賬號(hào),請(qǐng)點(diǎn)此處 <a href="0114login.php">登陸</a> </span>
  98. </div>
  99. </div>
  100. </body>
  101. </html>

4、驗(yàn)證頁(yè)面代碼:

  1. //驗(yàn)證來(lái)源的合法性
  2. $allow_urls=['0114login.php','0114register.php','0114index.php'];
  3. //echo $_SERVER['HTTP_REFERER'];
  4. $url=(filter_input(INPUT_SERVER,'HTTP_REFERER'));
  5. $url=basename($url);
  6. //echo $url;
  7. $users_dates=[
  8. ['username'=>'ldy','pwd'=>'7c4a8d09ca3762af61e59520943dc26494f8941b'],
  9. ['username'=>'cqw','pwd'=>'7c4a8d09ca3762af61e59520943dc26494f8941b']
  10. ];
  11. if (in_array($url,$allow_urls)) {
  12. // echo '在白名單';
  13. $action= filter_input(INPUT_GET,'action',FILTER_SANITIZE_STRING);
  14. $action=strtolower($action);
  15. //來(lái)判斷是登陸還是注冊(cè)
  16. switch ($action) {
  17. case 'login' ://登陸驗(yàn)證
  18. // echo $action;
  19. // echo $_POST['username'];
  20. // echo $_POST['pwd'];
  21. $name=filter_input(INPUT_POST,'username',FILTER_SANITIZE_STRING);
  22. $pwd=filter_input(INPUT_POST,'pwd',FILTER_SANITIZE_STRING);
  23. $pwd=sha1($pwd);
  24. $result=array_filter($users_dates,function($user)use($name,$pwd){
  25. return $name===$user['username']&&$pwd===$user['pwd'];
  26. });
  27. // print_r($result);
  28. if (count($result)===1) {
  29. $cook=serialize($result[0]);
  30. print_r(unserialize($cook));
  31. setcookie('user',$cook);
  32. exit('<script>alert("驗(yàn)證通過(guò)");location.href="0114index.php"</script>');
  33. } else {
  34. exit('<script>alert("郵箱或密碼錯(cuò)誤,或沒(méi)有帳號(hào)");location.href="0114register.php"</script>');
  35. }
  36. break;
  37. case 'logout' ://登出操作
  38. if(filter_input(INPUT_COOKIE,'user')){
  39. setcookie('user',null,time()-3600);
  40. exit('<script>alert("退出成功");location.href="0114index.php"</script>');
  41. }
  42. break;
  43. case 'register' ://注冊(cè)操作
  44. if (filter_has_var(INPUT_POST,'username')){
  45. $name=filter_input(INPUT_POST,'username',FILTER_SANITIZE_STRING);
  46. $password1=filter_input(INPUT_POST,'pwd1',FILTER_SANITIZE_STRING);
  47. $password2=filter_input(INPUT_POST,'pwd2',FILTER_SANITIZE_STRING);
  48. if($password1===$password2){
  49. $pwd=$password1;
  50. $pwd=sha1($pwd);
  51. }
  52. $data=compact('name','pwd');
  53. $users_dates[]=array_push($users_dates,$data);
  54. if(array_push($users_dates,$data)) {
  55. exit('<script>alert("注冊(cè)成功");location.assign("0114index.php");</script>');
  56. }
  57. }
  58. break;
  59. default:
  60. exit();
  61. }
  62. } else {
  63. echo '不在白名單';
  64. die;
  65. }

測(cè)試效果圖

1、首頁(yè)效果(未登陸)

2、首頁(yè)(登陸)

3、登陸和注冊(cè)頁(yè)面:


4、登陸頁(yè)面(登陸狀態(tài)進(jìn)入):

作業(yè)二session版

由于session版的效果跟cookies的一直,所以只上傳關(guān)鍵代碼
1、首頁(yè)代碼:(通過(guò)$_SESSION[];)判斷登陸狀態(tài)

  1. session_start();//開啟會(huì)話
  2. if (isset($_SESSION['user'])) {
  3. $users=$_SESSION['user']
  4. $status='退出';
  5. $status_url='0114verify.php?action=logout';
  6. }else {
  7. $users='用戶注冊(cè)';
  8. $user_url='0114register.php';
  9. $status='登陸';
  10. $status_url='0114login.php';
  11. }
  12. ?>

2、登錄頁(yè)代碼:(判斷登陸狀態(tài),禁止重復(fù)登陸)

  1. session_start();//開啟會(huì)話
  2. if (isset($_SESSION['user'])) {
  3. exit('<script>alert("請(qǐng)不要重復(fù)登錄");location.href="0114index.php"</script>');
  4. }

3、驗(yàn)證頁(yè)代碼:

  1. //1、先開啟session會(huì)話
  2. session_start();
  3. //驗(yàn)證來(lái)源合法性,和cookies一樣
  4. ………………
  5. //分發(fā)處理
  6. ……………………
  7. //1、登陸處理:創(chuàng)建session ,(通過(guò)session[];保存登錄信息到服務(wù)器)
  8. session['user']=$result;
  9. ……………………
  10. //2、退出處理:
  11. session_destroy();
  12. ……………………

總結(jié):

1、知識(shí)點(diǎn):

1、cookie相關(guān)知識(shí)點(diǎn):
(1)、setcookie('name','值信息','失效時(shí)間');
(2)、讀取cookie信息:$_COOKIE['變量名'];
(3)、刪除cookie信息:setcookie('name',null,time()-1);
2、session相關(guān)知識(shí)點(diǎn):
(1)、session_start();開啟會(huì)話,調(diào)用session必須先開啟會(huì)話
(2)、讀取session信息:$_SESSION['變量名'];
(3)、結(jié)束當(dāng)前會(huì)話刪除session會(huì)話信息:session_destroy();
unset($_SESSION['']);注銷單個(gè)會(huì)話
3、相關(guān)知識(shí)補(bǔ)充:
(1)、過(guò)濾函數(shù):($_SERVER['HTTP_ERFERER']進(jìn)入當(dāng)前頁(yè)面的上一個(gè)頁(yè)面的url地址;)
filter_has_var();判斷是否存在
filter_input();
filter_var();
(2)、serialize();序列化,寫入cookie時(shí)
unserialize(); 反序列化,讀取cookie時(shí)需要先反序列化
(3)、數(shù)組操作:
array_push($arr,$a);將$a入棧$arr;
array_pop($arr);將$arr最后一個(gè)元素出棧;
array_filter($arr,callback);$arr要循環(huán)的數(shù)組,callback回調(diào)函數(shù),返回過(guò)濾后符合回調(diào)函數(shù)的數(shù)組

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

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

老師批語(yǔ):界面實(shí)在找不到合適的詞來(lái)表?yè)P(yáng), 非常有個(gè)性.... 功能寫得不錯(cuò)
本博文版權(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é)