登录  /  注册
博主信息
博文 145
粉丝 7
评论 7
访问量 159495
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
01月14日作业:登陆注册验证案例作业(cookie和session)
李东亚¹⁸⁰³⁹⁵⁴⁰¹²⁰
原创
994人浏览过

作业一 cookie版

1、代码:

1、首页代码

  1. //判断登陆状态,输出显示登陆状态变量(没有学JS利用PHP的if语句来判断输出登陆和登出状态)
  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='用户注册';
  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>种业圈</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. //根据判断输出的变量信息显示登陆状态
  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、登陆页面代码:

  1. if (filter_has_var(INPUT_COOKIE,'user')) {
  2. exit('<script>alert("请勿重复登陆");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="请输入你的账号" required>
  77. <label for="pwd1"><span>密 码:</span></label>
  78. <input type="password" name="pwd" id="pwd" placeholder="请输入你的密码" required>
  79. <input type="submit" value="登陆">
  80. <input type="reset" value="重置">
  81. </form>
  82. <div>
  83. <span>没有账号,请点此处 <a href="0114register.php">注册</a> </span>
  84. </div>
  85. </div>
  86. </body>
  87. </html>

3、注册页面代码(利用之前做的页面没有做修改,但只利用用户名和密码1、密码2三项,其他项不考虑):

  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>新用户注册</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="请输入你的账号" required>
  73. <label for="pwd1"><span>密 码:</span></label>
  74. <input type="password" name="pwd1" id="pwd1" placeholder="请输入你的密码" required>
  75. <label for="pwd2"><span>密 码:</span></label>
  76. <input type="password" name="pwd2" id="pwd2" placeholder="请再次输入你的密码" required>
  77. <label for="email"><span>邮箱</span></label>
  78. <input type="email" name="email" id="email" placeholder="请输入邮箱" required>
  79. <label for="tel"><span>电话</span></label>
  80. <input type="tel" name="tel" id="tel" placeholder="请输入手机号" 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>兴趣爱好</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="注册">
  94. <input type="reset" value="重置">
  95. </form>
  96. <div>
  97. <span>已有账号,请点此处 <a href="0114login.php">登陆</a> </span>
  98. </div>
  99. </div>
  100. </body>
  101. </html>

4、验证页面代码:

  1. //验证来源的合法性
  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. //来判断是登陆还是注册
  16. switch ($action) {
  17. case 'login' ://登陆验证
  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("验证通过");location.href="0114index.php"</script>');
  33. } else {
  34. exit('<script>alert("邮箱或密码错误,或没有帐号");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' ://注册操作
  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("注册成功");location.assign("0114index.php");</script>');
  56. }
  57. }
  58. break;
  59. default:
  60. exit();
  61. }
  62. } else {
  63. echo '不在白名单';
  64. die;
  65. }

测试效果图

1、首页效果(未登陆)

2、首页(登陆)

3、登陆和注册页面:


4、登陆页面(登陆状态进入):

作业二session版

由于session版的效果跟cookies的一直,所以只上传关键代码
1、首页代码:(通过$_SESSION[];)判断登陆状态

  1. session_start();//开启会话
  2. if (isset($_SESSION['user'])) {
  3. $users=$_SESSION['user']
  4. $status='退出';
  5. $status_url='0114verify.php?action=logout';
  6. }else {
  7. $users='用户注册';
  8. $user_url='0114register.php';
  9. $status='登陆';
  10. $status_url='0114login.php';
  11. }
  12. ?>

2、登录页代码:(判断登陆状态,禁止重复登陆)

  1. session_start();//开启会话
  2. if (isset($_SESSION['user'])) {
  3. exit('<script>alert("请不要重复登录");location.href="0114index.php"</script>');
  4. }

3、验证页代码:

  1. //1、先开启session会话
  2. session_start();
  3. //验证来源合法性,和cookies一样
  4. ………………
  5. //分发处理
  6. ……………………
  7. //1、登陆处理:创建session ,(通过session[];保存登录信息到服务器)
  8. session['user']=$result;
  9. ……………………
  10. //2、退出处理:
  11. session_destroy();
  12. ……………………

总结:

1、知识点:

1、cookie相关知识点:
(1)、setcookie('name','值信息','失效时间');
(2)、读取cookie信息:$_COOKIE['变量名'];
(3)、删除cookie信息:setcookie('name',null,time()-1);
2、session相关知识点:
(1)、session_start();开启会话,调用session必须先开启会话
(2)、读取session信息:$_SESSION['变量名'];
(3)、结束当前会话删除session会话信息:session_destroy();
unset($_SESSION['']);注销单个会话
3、相关知识补充:
(1)、过滤函数:($_SERVER['HTTP_ERFERER']进入当前页面的上一个页面的url地址;)
filter_has_var();判断是否存在
filter_input();
filter_var();
(2)、serialize();序列化,写入cookie时
unserialize(); 反序列化,读取cookie时需要先反序列化
(3)、数组操作:
array_push($arr,$a);将$a入栈$arr;
array_pop($arr);将$arr最后一个元素出栈;
array_filter($arr,callback);$arr要循环的数组,callback回调函数,返回过滤后符合回调函数的数组

批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:界面实在找不到合适的词来表扬, 非常有个性.... 功能写得不错
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学