Blogger Information
Blog 32
fans 0
comment 0
visits 27412
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP 会话控制
Yang_Sir
Original
835 people have browsed it

1.什么是会话控制

  • 会话指的用户的浏览器与服务器的信息交互
  • 会话控制就是指控制保存浏览器与服务器的通话状态,
  • 会话控制分两种方式:cookiesession
  • cookie把信息保存在浏览器
  • session把信息保存在服务器
  • cookie保存在浏览器,session保存在服务器

设置和销毁cookie:

  1. //setcookie(名称, 值, 过期时间);时间格式为时间戳
  2. setcookie('username','admin', time()+60*10);
  3. //获取cookie中的值
  4. $_COOKIE['username'];
  5. //销毁cookie,过去时间设置为过去时间
  6. setcookie('username','admin', time()-10);

设置和销毁session:

  1. //开启seeion会话
  2. session_start();
  3. //设置session
  4. $_SESSION['username'] ='admin';
  5. //获取session中的值
  6. $_SESSION['username'];
  7. //销毁session,可以使用session_destroy()、session_unset()
  8. session_destroy();

3.实例演示

做一个演示页面,完成登录注册的功能
分别使用cookie和session两种方式保存登录信息

1.首页

  1. <?php session_start();//开启session会话 ?>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>首页</title>
  8. <style>
  9. body>nav {
  10. display: flex;
  11. flex-flow: row nowrap;
  12. justify-content: space-between;
  13. align-items: center;
  14. background-color: #400040;
  15. /* width: 93vw; */
  16. height: 80px;
  17. padding: 20px 60px;
  18. }
  19. a {
  20. text-decoration: none;
  21. color: aliceblue;
  22. font-size: 20px;
  23. }
  24. span>a {
  25. text-decoration: none;
  26. color: red;
  27. font-size: 18px;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <!-- 导航栏 -->
  33. <nav>
  34. <a href="index.php">首页</a>
  35. <?php ///*使用cookie方式*/ if(isset($_COOKIE['nickname'])):?>
  36. <?php /*使用session方式*/ if(isset($_SESSION['nickname'])):?>
  37. <span><a href='#'>欢迎您!
  38. <?php //echo $_COOKIE['nickname'];?>
  39. <?php echo $_SESSION['nickname'];?>
  40. </a>&nbsp;&nbsp;<a href="handle.php?act=logout">退出</a></span>
  41. <?php else:?>
  42. <a href='login.php'>登录/注册</a>
  43. <?php endif ?>
  44. </nav>
  45. </body>
  46. </html>

效果图
登录前:
登录后

2.登录页

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>登录</title>
  7. <style>
  8. body {
  9. display: grid;
  10. justify-content: center;
  11. align-items: center;
  12. background-image: url('bg1.jpg');
  13. background-size: 100vw 100vh;
  14. }
  15. .form {
  16. margin-top: 220px;
  17. width: 500px;
  18. height: 300px;
  19. background-color: #87a9c5;
  20. border-radius: 5%;
  21. display: flex;
  22. flex-flow: column nowrap;
  23. justify-content: space-around;
  24. }
  25. section {
  26. padding: 0 30px;
  27. display: flex;
  28. align-items: center;
  29. justify-content: center;
  30. }
  31. section > input {
  32. height: 40px;
  33. flex-grow: 1;
  34. font-size: 1.5em;
  35. }
  36. section > label,
  37. button {
  38. width: 80px;
  39. font-size: 1.5em;
  40. /* flex-grow: 3; */
  41. }
  42. .form>:last-of-type{
  43. margin-left: 30px ;
  44. display: flex;
  45. align-items: center;
  46. justify-content: space-evenly;
  47. }
  48. </style>
  49. </head>
  50. <body>
  51. <form action="handle.php?act=login" method="POST" class="form">
  52. <section>
  53. <label for="username">账号:</label
  54. ><input type="text" name="username" id="username" required/>
  55. </section>
  56. <section>
  57. <label for="password">密码:</label
  58. ><input type="password" name="password" id="password" required/>
  59. </section>
  60. <section>
  61. <button type="submit">登录</button>
  62. <span>没有账号?点击<a href="register.php">注册</a>
  63. </section>
  64. </form>
  65. </body>
  66. </html>

效果图

3.注册页

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>注册</title>
  7. <style>
  8. body {
  9. background-color: #d4f3d4;
  10. display: grid;
  11. justify-content: center;
  12. align-items: center;
  13. background-image: url();
  14. }
  15. .form {
  16. background-color: #acd6ac;
  17. border: 1px solid #cccccc;
  18. margin-top: 30px;
  19. width: 500px;
  20. height: 700px;
  21. /* background-color: #cad6ca; */
  22. border-radius: 2%;
  23. display: grid;
  24. grid-template-columns: 1fr;
  25. grid-template-rows: repeat(9, 1fr);
  26. }
  27. section {
  28. height: 50px;
  29. /* border-bottom: 1px solid;
  30. border-top: 1px solid; */
  31. padding: 0 30px;
  32. display: flex;
  33. flex-flow: row nowrap;
  34. align-items: center;
  35. justify-content: center;
  36. }
  37. section>input {
  38. height: 30px;
  39. flex-grow: 1;
  40. font-size: 1.2em;
  41. }
  42. section>textarea {
  43. height: 60px;
  44. flex-grow: 1;
  45. resize: none;
  46. }
  47. section>label,
  48. button {
  49. color: rgb(105, 76, 55);
  50. width: 120px;
  51. font-size: 1.3em;
  52. text-align: center;
  53. /* flex-grow: 3; */
  54. }
  55. .form> :last-of-type {
  56. margin-left: 30px;
  57. display: flex;
  58. align-items: center;
  59. justify-content: space-evenly;
  60. }
  61. h2 {
  62. color: brown;
  63. }
  64. </style>
  65. </head>
  66. <body>
  67. <form action="handle.php?act=register" method="POST" class="form">
  68. <section>
  69. <h2>用户注册</h2>
  70. </section>
  71. <section>
  72. <label for="username">账号:</label><input type="text" name="username" id="username" autofocus />
  73. </section>
  74. <section>
  75. <label for="nickname">昵称:</label><input type="text" name="nickname" id="nickname" required />
  76. </section>
  77. <section>
  78. <label for="password1">密码:</label><input type="password" name="password1" id="password1" required />
  79. </section>
  80. <section>
  81. <label for="password2">重复密码:</label><input type="password" name="password2" id="password2" required />
  82. </section>
  83. <section>
  84. <label for="email">邮箱:</label><input type="email" name="email" id="email" required />
  85. </section>
  86. <section>
  87. <label for="phone">电话:</label><input type="tel" name="phone" id="phone" />
  88. </section>
  89. <section>
  90. <label for="replay">个人说明:</label><textarea type="text" name="replay" id="replay" /></textarea>
  91. </section>
  92. <section>
  93. <button type="submit">提交</button>
  94. <button type="reset">重置</button>
  95. <span>已有账号?<a href="login.php">去登录</a></span>
  96. </section>
  97. </form>
  98. </body>
  99. </html>

效果图


4.请求处理页

  1. <?php
  2. session_start();//开启session会话
  3. if(!isset($_GET['act'])){
  4. exit('<script>alert("404!,未找到指向文件");location.href="index.php";</script>');
  5. }
  6. $dsn = "mysql:host=localhost;dbname=www.merchant.office;charset=utf8";
  7. $sql_name = "merchant";
  8. $sql_password = "merchant";
  9. // $dsn,$sql_name,$sql_password
  10. $action = $_GET['act'];
  11. switch($action){
  12. case 'logout':
  13. setcookie('nickname',null,time()-10);//设置cookie过期,即销毁
  14. session_destroy();//删除session
  15. Header("location:index.php");
  16. break;
  17. case 'login' :
  18. if(isset($_POST['username'])){
  19. $username = $_POST['username'];
  20. $password = md5($_POST['password']);
  21. $sql = "select `nickname` from user where `username`='{$username}' and `password`='{$password}'";
  22. try{
  23. $pdo = new pdo($dsn,$sql_name,$sql_password);
  24. $stmt = $pdo->prepare($sql);
  25. $stmt->execute();
  26. $res = $stmt->fetchAll();
  27. if(!empty($res)){
  28. setcookie('nickname',$res[0]['nickname'],time()+3600);//设置cookie
  29. $_SESSION['nickname'] = $res[0]['nickname'];//设置session
  30. header("location:index.php");
  31. }else{
  32. exit('<script>alert("登录失败,请检查账号密码!");location.href="login.php";</script>');
  33. }
  34. }catch(Exception $e){
  35. $err = $e->getMessage();
  36. exit("<script>alert('{$err}');location.href='login.php';</script>");
  37. }
  38. }else{
  39. exit('<script>alert("请正确输入用户名和密码!");location.href="login.php";</script>');
  40. }
  41. break;
  42. case 'register':
  43. if(isset($_POST['username'])&&isset($_POST['nickname'])&&$_POST['password1']===$_POST['password2']){
  44. extract($_POST);
  45. $time = time();
  46. $password=md5($_POST['password1']);
  47. $sql = "insert user set `username`='{$username}',`password`='{$password}',
  48. `nickname`='{$nickname}',`email`='{$email}',`replay`='{$replay}',`phone`='{$phone}',`create_time`='{$time}',`update_time`='{$time}'";
  49. try{
  50. $pdo = new pdo($dsn,$sql_name,$sql_password);
  51. $stmt = $pdo->prepare($sql);
  52. $stmt->execute();
  53. if($stmt->rowCount()===1){
  54. exit('<script>alert("注册成功");location.assign("login.php")</script>');
  55. }else{
  56. exit('<script>alert("注册失败");location.assign("register.php")</script>');
  57. }
  58. }catch(Exception $e){
  59. $err = $e->getMessage();
  60. exit("<script>alert('{$err}');location.href='register.php';</script>");
  61. }
  62. }else{
  63. exit('<script>alert("资料提交有误!");location.href="register.php";</script>');
  64. }
  65. break;
  66. default:
  67. exit('<script>alert("未知指令")</script>');
  68. }

4. 总结

  • cookie和session都可以保存信息,只不过保存的位置不一样。
  • 记录用户信息的状态,在多个页面跳转时使用能提升用户感受
  • cookie文件保存在用户本地,可能会被篡改,不用于保存重要信息
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!