Blogger Information
Blog 27
fans 0
comment 0
visits 20815
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
cookie与session实战-2019年11月22日
古城老巷丶
Original
613 people have browsed it

SESSION 登录实战

index首页文件
  1. <?php
  2. // 开启session会话
  3. session_start();
  4. // 判断用户是否登录,如果没有,则跳转到相应的页面
  5. // var_dump($_SESSION['name']);
  6. // die;
  7. if (isset($_SESSION['name'])) {
  8. echo '用户: ' . $_SESSION['name'] . '已登录<br>';
  9. echo '<a href="logout.php">退出</a>';
  10. } else {
  11. // 2. 未登录,就跳转到登录页面
  12. echo '<a href="login.php">请登录</a>';
  13. }

login登录页

  1. <?php
  2. // 防止用户重复登录
  3. if (isset($_SESSION['name'])) {
  4. session_start();
  5. # code...
  6. echo '<script>alert("不要重复登录");location.assign("index.php");</script>';
  7. }
  8. ?>
  9. <!DOCTYPE html>
  10. <html lang="en">
  11. <head>
  12. <meta charset="UTF-8">
  13. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  14. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  15. <title>登录</title>
  16. </head>
  17. <body>
  18. <h3>用户登录</h3>
  19. <form action="check.php" method="post" onsubmit="return isEmpty();">
  20. <p>
  21. <label for=" phone">手机:</label>
  22. <input type="phone" name="phone" id="phone">
  23. </p>
  24. <p>
  25. <label for="password">密码:</label>
  26. <input type="password" name="password" id="password">
  27. </p>
  28. <p>
  29. <button>提交</button>
  30. </p>
  31. </form>
  32. <script>
  33. function isEmpty() {
  34. var phone = document.getElementById('phone').value;
  35. var password = document.getElementById('password').value;
  36. if (phone.length === 0 || password.length === 0) {
  37. alert('手机和密码不能为空');
  38. return false;
  39. }
  40. }
  41. </script>
  42. </body>
  43. </html>

check登录验证

  1. <?php
  2. require 'connect.php';
  3. session_start();
  4. // 1.判断用户的请求类型是否正确?
  5. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  6. // 获取表单提交的数据
  7. $user_name = $_POST['phone'];
  8. $user_alias = md5($_POST['password']);
  9. // var_dump($user_alias);
  10. // die;
  11. // 3. 用用户表user.dbf进行验证
  12. $sql = 'SELECT * FROM `user` WHERE `phone` = :phone AND `pwd` = :password LIMIT 1';
  13. $stmt = $pdo->prepare($sql);
  14. $stmt->execute(['phone' => $user_name, 'password' => $user_alias]);
  15. $user = $stmt->fetch(PDO::FETCH_ASSOC);
  16. // var_dump($user['name']);
  17. // die;
  18. // 4. 判断验证的结果
  19. if (false === $user) {
  20. // 验证失败,返回上一下页面
  21. echo '<script>alert("验证失败");history.back();</script>';
  22. die;
  23. }
  24. // 验证成功,将用户的信息写到session
  25. $_SESSION['name'] = $user['name'];
  26. // var_dump($_SESSION['name']);
  27. // die;
  28. echo '<script>alert("登录成功");location.assign("index.php");</script>';
  29. exit;
  30. } else {
  31. echo '请求类型错误';
  32. }

logout退出

  1. <?php
  2. // 必须在用户已经登录的情况下再退出
  3. session_start();
  4. if (isset($_SESSION['name'])) {
  5. session_destroy();
  6. echo '<script>alert("退出成功");location.assign("index.php");</script>';
  7. } else {
  8. // 要求用户先登录
  9. echo '<script>alert("请先登录");location.assign("login.php");</script>';
  10. }

效果图

数据库:

登录前

用户登录页

验证页

登录成功页

总结

cookie与session提供两个页面通信,他们在在多个页面存储一些公共变量,每个页面都能访问这些
变量,实现多个页面的通信作用。
cookie的数据存于客户端,session的数据一般存于服务端。

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