Blogger Information
Blog 119
fans 3
comment 1
visits 94698
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP会话控制实战cookie、session------PHP培训十期线上班 学号:510251 01月14日作业
赵大叔
Original
782 people have browsed it

PHP会话控制实战cookie、session

1、数据保存在客户端浏览器上,如果浏览器关闭cookie,则无法使用
2、创建cookie:setcookie(名称,值,[过期时间])
3、使用cookie:$_COOKIE['名称']
4、删除cookie:为cookie设置一个已经过期的时间,如:setcookie(名称,值,time()-1)

1.index

  1. <?php
  2. //判断是否登录
  3. if(filter_has_var(INPUT_COOKIE, 'user')){
  4. $user = unserialize(filter_input(INPUT_COOKIE, 'user'));
  5. //print_r($user);
  6. }
  7. ?>
  8. <!doctype html>
  9. <html lang="en">
  10. <head>
  11. <meta charset="UTF-8">
  12. <title>首页</title>
  13. <link rel="stylesheet" type="text/css" href="css/index.css">
  14. </head>
  15. <body>
  16. <nav>
  17. <a href="">首页</a>
  18. <?php if(isset($user)): ?>
  19. <a href="" id="logout">
  20. <span><?php echo $user['name'] ?></span>安全退出
  21. </a>
  22. <?php else: ?>
  23. <a href="login.php">登录</a>
  24. <?php endif;?>
  25. </nav>
  26. <script>
  27. // 为退出按钮创建事件监听器
  28. if (document.querySelector('#logout') !== null) {
  29. document.querySelector('#logout').addEventListener('click', function(event) {
  30. if (confirm('是否退出')) {
  31. // 禁用默认行为, 其实就是禁用原<a>标签的点击跳转行为,使用事件中的自定义方法处理
  32. event.preventDefault();
  33. // 跳转到退出事件处理器
  34. window.location.assign('handle.php?action=logout');
  35. }
  36. });
  37. }
  38. </script>
  39. </body>
  40. </html>

2.login

  1. <?php
  2. // 判断是否已登录?
  3. if (filter_has_var(INPUT_COOKIE, 'user')) {
  4. exit('<script>alert("请不要重复登录");location.href="index.php"</script>');
  5. }
  6. ?>
  7. <!doctype html>
  8. <html lang="en">
  9. <head>
  10. <meta charset="UTF-8">
  11. <title>用户登录</title>
  12. <link rel="stylesheet" type="text/css" href="css/style.css">
  13. </head>
  14. <body>
  15. <h3>用户登录</h3>
  16. <form action="handle.php?action=login" method="post">
  17. <div>
  18. <label for="email">邮箱:</label>
  19. <input type="email" name="email" id="email" placeholder="zhangsan@email.com" required autofocus>
  20. </div>
  21. <div>
  22. <label for="password">密码:</label>
  23. <input type="password" name="password" id="password" placeholder="不少于6个字符">
  24. </div>
  25. <div>
  26. <button>提交</button>
  27. </div>
  28. </form>
  29. <a href="register.php">还没有帐号,点击注册</a>
  30. </body>
  31. </html>

3.register

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>用户注册</title>
  6. <link rel="stylesheet" type="text/css" href="css/style.css">
  7. </head>
  8. <body>
  9. <h3>用户注册</h3>
  10. <form action="handle.php?action=register" method="post" onsubmit="return compare()">
  11. <div>
  12. <label for="name">用户名:</label>
  13. <input type="text" name="name" id="name" placeholder="zhangsan" required autofocus>
  14. </div>
  15. <div>
  16. <label for="email">邮箱:</label>
  17. <input type="email" name="email" id="email" placeholder="zhangsan@email.com" required>
  18. </div>
  19. <div>
  20. <label for="password1">密码:</label>
  21. <input type="password" name="password1" id="password1" placeholder="不少于6个字符">
  22. </div>
  23. <div>
  24. <label for="password2">重复密码:</label>
  25. <input type="password" name="password2" id="password2" placeholder="和上面输入一致">
  26. </div>
  27. <div>
  28. <button>提交</button><span id="tips" style="color: red"></span>
  29. </div>
  30. </form>
  31. <a href="login.php">已有帐号,点击登录</a>
  32. <script>
  33. // 验证二次密码是否相等?JS 课堂老师复制代码少button的id
  34. function compare() {
  35. if (document.forms[0].password1.value.trim() !== document.forms[0].password2.value.trim()) {
  36. document.querySelector('#tips').innerText = '二次密码不相等';
  37. return false;
  38. }
  39. }
  40. </script>
  41. </body>
  42. </html>

4.handle

  1. <?php
  2. // 用户资料库, 实际项目中,应该用数据库
  3. $users = [
  4. [
  5. 'id' => 1,
  6. 'name' => 'admin',
  7. 'email' => 'admin@php.cn',
  8. //sha1()加密密码
  9. 'password' => '7c4a8d09ca3762af61e59520943dc26494f8941b',
  10. ],
  11. [
  12. 'id' => 2,
  13. 'name' => 'dashu',
  14. 'email' => 'dashu@php.cn',
  15. 'password' => '7c4a8d09ca3762af61e59520943dc26494f8941b',
  16. ],
  17. ];
  18. // 1. 验证请求来源的合法性
  19. // 设置合法请求地址的白名单
  20. $allowUrls = ['index.php', 'login.php', 'register.php'];
  21. // 获取当前的请求入口地址
  22. //basename():获取当前请求脚本名称
  23. $currentUrl = basename(filter_input(INPUT_SERVER, 'HTTP_REFERER'));
  24. //in_array(),判断当前请求在不在白名单
  25. if(!in_array($currentUrl, $allowUrls)){
  26. echo '非法来源';
  27. }else{
  28. //echo '合法来源';
  29. }
  30. // 2.进行请求分发处理
  31. //获取当前请求
  32. //echo $_GET['action'];
  33. //过滤处理, strtolower():字符串转为小写
  34. $action = strtolower(filter_input(INPUT_GET, 'action', FILTER_SANITIZE_STRING));
  35. //switch判断当前请求
  36. switch($action){
  37. //登录
  38. case 'login':
  39. //判断是否是POST请求?
  40. if(filter_input(INPUT_SERVER, 'REQUEST_METHOD') === 'POST'){
  41. //获取当前请求的值:+清理特殊字符
  42. $email = filter_var(filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL), FILTER_SANITIZE_EMAIL);
  43. $password = sha1(filter_input(INPUT_POST, 'password'));
  44. //echo $email, $password;
  45. //array_filter从数组中获取满足条件的值
  46. $results = array_filter($users, function ($user)use($email, $password){
  47. return $email === $user['email'] && $password === $user['password'];
  48. });
  49. //print_r($results);
  50. if(count($results) === 1) {
  51. //判断满足条件的指令数量=1?设置cookie
  52. //array_pop()=$results[0]
  53. setcookie('user', serialize(array_pop($results)));
  54. //print_r(unserialize($_COOKIE['user']));
  55. exit('<script>alert("验证通过");location.href="index.php"</script>');
  56. }else{
  57. exit('<script>alert("邮箱或密码错误,或没有帐号");location.href="register.php"</script>');
  58. }
  59. }else{
  60. exit('非法请求。');
  61. }
  62. break;
  63. //退出
  64. case 'logout':
  65. if(filter_input(INPUT_COOKIE,'user')){
  66. setcookie('user',null,time()-3600);
  67. exit('<script>alert("退出成功");location.href="index.php"</script>');
  68. }
  69. break;
  70. //注册
  71. case 'register':
  72. $name = filter_var(filter_input(INPUT_POST, 'name'), FILTER_SANITIZE_SPECIAL_CHARS);
  73. $email = filter_var(filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL), FILTER_SANITIZE_EMAIL);
  74. $password = sha1(filter_input(INPUT_POST, 'password1'));
  75. $id = 3;
  76. $data = compact('id', 'name', 'email', 'password');
  77. //添加
  78. //$users[] = $data;
  79. //array_push($users,$data);
  80. if(array_push($users, $data) === 1){
  81. exit('<script>alert("注册成功");location.href="index.php"</script>');
  82. }
  83. print_r($users);
  84. break;
  85. //未定义操作
  86. default:
  87. exit('未定义操作');
  88. }

首页:

登录:

注册:

SESSION

1、数据保存在服务器上
2、启动session:session_start()
3、创建session:$_SESSION['名称']=值
4、使用session:$_SESSION['名称']
5、删除单个session:unset($_SESSION['名称'])
6、删除所有的session:session_unset()
7、销毁session:session_destory()
1.handle

  1. <?php
  2. session_start();
  3. if(count($results) === 1) {
  4. //判断满足条件的指令数量=1?设置cookie
  5. //array_pop()=$results[0]
  6. //setcookie('user', serialize(array_pop($results)));
  7. //session
  8. $_SESSION['user'] = array_pop($results);
  9. //print_r(unserialize($_COOKIE['user']));
  10. case 'logout':
  11. if(isset($_SESSION['user'])){
  12. //setcookie('user',null,time()-3600);
  13. //session
  14. session_destroy();
  15. exit('<script>alert("退出成功");location.href="index.php"</script>');
  16. }
  17. break;

2.index

  1. <?php
  2. //判断是否登录
  3. /*if(filter_has_var(INPUT_COOKIE, 'user')){
  4. $user = unserialize(filter_input(INPUT_COOKIE, 'user'));
  5. //print_r($user);
  6. }*/
  7. //session
  8. //开启
  9. session_start();
  10. if(isset($_SESSION['user'])){
  11. $user = $_SESSION['user'];
  12. }
  13. ?>

3.login

  1. // 判断是否已登录?
  2. /*if (filter_has_var(INPUT_COOKIE, 'user')) {
  3. exit('<script>alert("请不要重复登录");location.href="index.php"</script>');
  4. }*/
  5. session_start();
  6. if($_SESSION['user']) {
  7. exit('<script>alert("请不要重复登录");location.href="index.php"</script>');
  8. }
  9. ?>

登录验证流程图:

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:会话控制很有用, 咱们现在学的是底层, 不当框架做了封装,你很难看到底层实现了
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post