Blogger Information
Blog 27
fans 0
comment 0
visits 17357
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
020-11月22日-PHP第十节-fetch() in PDOStatement类及cookie\session会话控制
冇忉丼
Original
627 people have browsed it

1. SESSION 登录实战

项目文件目录如下图:

首先请求派发器:dispatch.php (即定义index.php?action=xx)

  1. <?php
  2. session_start();
  3. //在该脚本中打开会话,check,logout,login均无需再打开
  4. // 连接数据库
  5. require __DIR__ . '/connect.php';
  6. // 获取请求参数
  7. $action = isset($_GET['action']) ? $_GET['action'] : 'login';
  8. $action = htmlentities(strtolower(trim($action)));
  9. // 请求分发
  10. switch ($action) {
  11. // 1. 登录页面
  12. case 'login':
  13. // 加载登录表单
  14. include __DIR__ . '/login.php';
  15. break;
  16. // 2. 验证登录
  17. case 'check':
  18. include __DIR__ . '/check.php';
  19. break;
  20. // 3. 退出登录
  21. case 'logout':
  22. include __DIR__ . '/logout.php';
  23. break;
  24. // 默认操作
  25. default:
  26. header('Location: index.php');
  27. echo '<script>location.assign("index.php");</script>';
  28. //默认跳到index页面
  29. }

index.php主页页面:

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

login.php登陆页面定义了post:

  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 post数据与数据库数据比对

  1. <?php
  2. // 1.判断用户的请求类型是否正确?
  3. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  4. // 2.获取表单数据
  5. $phone = $_POST['phone'];
  6. $password = sha1($_POST['pwd']);
  7. // 3. 用用户表user.dbf进行验证
  8. $sql = 'SELECT * FROM `user` WHERE `phone` = :phone AND `pwd` = :pwd LIMIT 1';
  9. $stmt = $pdo->prepare($sql);
  10. $stmt->execute(['phone'=>$phone, 'pwd'=>$pwd]);
  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. }

connect.php连接数据库:

  1. <?php
  2. $db = [
  3. 'type' => 'mysql',
  4. 'host' => 'localhost',
  5. 'dbname' => 'anguoguo',
  6. 'username' => 'root',
  7. 'password' => 'root'
  8. ];
  9. $dsn = "{$db['type']}:host={$db['host']};dbname={$db['dbname']}";
  10. $username = $db['username'];
  11. $password = $db['password'];
  12. try {
  13. $pdo = new PDO($dsn, $username, $password);
  14. } catch (PDOException $e) {
  15. die('连接失败' . $e->getMessage());
  16. }

2. 练熟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