Blogger Information
Blog 11
fans 0
comment 0
visits 27453
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php会话流程控制cookie和session的应用作业
饮雪煮茶
Original
723 people have browsed it

流程结构

三个静态页

首页效果图

注册页面效果图

登录页面效果图

首页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个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
  13. <title>网站首页</title>
  14. <!-- Bootstrap -->
  15. <link rel="stylesheet" href="./css/index.css">
  16. </head>
  17. <body>
  18. <nav>
  19. <div class="container">
  20. <a href="">我的网站</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("已经登录请不要重复登录!"); 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个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
  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="请输入邮箱">
  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="请输入密码">
  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">还没有账号,注册一个。</a></span>
  43. </form>
  44. </body>
  45. </html>

注册页面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个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
  8. <title>注册新用户</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>注册新用户</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">重复密码:</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">已经有帐号了,直接登录。</a></span>
  50. </form>
  51. </body>
  52. </html>

控制页面 haddle.php源码

  1. <?php
  2. //用户数据组
  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. //获取当前访问来源地址文件名
  11. $currentUrl = basename(filter_input(INPUT_SERVER, 'HTTP_REFERER'));
  12. //判断是否再白名单内
  13. if (!in_array($currentUrl, $allowUrl)) {
  14. exit('非法请求地址');
  15. }
  16. //获取当前url访问action参数值
  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("登录失败,用户名或者密码错误,请重新登录"); location.href = "login.php";</script>');
  35. }
  36. } else {
  37. exit('非法请求!');
  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("用户名注册成功"); 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. }

两个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实现会话控制

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个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
  14. <title>网站首页</title>
  15. <!-- Bootstrap -->
  16. <link rel="stylesheet" href="./css/index.css">
  17. </head>
  18. <body>
  19. <nav>
  20. <div class="container">
  21. <a href="">我的网站</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("已经登录请不要重复登录!"); 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个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
  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="请输入邮箱">
  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="请输入密码">
  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">还没有账号,注册一个。</a></span>
  44. </form>
  45. </body>
  46. </html>

控制页面handle.php源码

  1. <?php
  2. session_start();
  3. //用户数据组
  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. //获取当前访问来源地址文件名
  12. $currentUrl = basename(filter_input(INPUT_SERVER, 'HTTP_REFERER'));
  13. //判断是否再白名单内
  14. if (!in_array($currentUrl, $allowUrl)) {
  15. exit('非法请求地址');
  16. }
  17. //获取当前url访问action参数值
  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("登录失败,用户名或者密码错误,请重新登录"); location.href = "login.php";</script>');
  37. }
  38. } else {
  39. exit('非法请求!');
  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("用户名注册成功"); 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相同。

Correction status:Uncorrected

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