Blogger Information
Blog 34
fans 2
comment 0
visits 23166
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
11月23号作业 SESSION实战
遗忘了寂寞
Original
603 people have browsed it

SESSION实战

index.php

  1. <?php
  2. //开启会话
  3. session_start();
  4. // 为简化程序, 使用了一个中间层: 请求派发器,类似于框架的控制器, 对用户的请求进行集中处理
  5. // 1: 已登录: 显示出用户的登录信息, 显示退出按钮
  6. if (isset($_SESSION['name']) ) {
  7. echo '用户: ' . $_SESSION['name'] . '已登录<br>';
  8. echo '<a href="dispatch.php?action=logout">退出</a>';
  9. } else {
  10. // 2. 未登录,就跳转到登录页面
  11. echo '<a href="dispatch.php?action=login">请登录</a>';
  12. }

派发器:dispatch.php

  1. <?php
  2. session_start();
  3. // 请求派发器: 前端控制器
  4. // 功能就是获取到用户的请求, 并调用不同的脚本进行处理和响应
  5. // 连接数据库
  6. require __DIR__ . '/pdo.php';
  7. // 获取请求参数
  8. $action = isset($_GET['action']) ? $_GET['action'] : 'login';
  9. $action = htmlentities(strtolower(trim($action)));
  10. // 请求分发
  11. switch ($action) {
  12. // 1. 登录页面
  13. case 'login':
  14. // 加载登录表单
  15. include __DIR__ . '/login.php';
  16. break;
  17. // 2. 验证登录
  18. case 'check':
  19. include __DIR__ . '/check.php';
  20. break;
  21. // 3. 退出登录
  22. case 'logout':
  23. include __DIR__ . '/logout.php';
  24. break;
  25. // 默认操作
  26. default:
  27. header('Location: index.php');
  28. echo '<script>location.assign("index.php");</script>';
  29. }

登陆页面:login.php

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

验证登陆:check.php

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

退出登陆:logout.php

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

练熟pdo操作,增删查改(手写)






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