Blogger Information
Blog 39
fans 0
comment 0
visits 30556
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP:会话控制,初试前后台数据交互实战
Original
728 people have browsed it

会话控制

1.index01.php
<?php
// 判断是否在登录状态
if(isset($_COOKIE[‘user’])) $user = unserialize ($_COOKIE[‘user’]);
?>

  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. <link rel="stylesheet" type="text/css" href="../css/index01.css">
  8. </head>
  9. <body>
  10. <nav>
  11. <a href="index01.php">我的论坛</a>
  12. <?php if(isset($user)): ?>
  13. <a href="" id="logout01"><span style="color:red"><?php echo $user['name']?></span>&nbsp;&nbsp;退出</a>
  14. <?php else: ?>
  15. <a href="login01.php">登录</a>
  16. <?php endif ?>
  17. </nav>
  18. </body>
  19. <script>
  20. // 为退出按钮创建事件监听器
  21. document.querySelector('#logout01').addEventListener('click', function(event) {
  22. if (confirm('是否退出')) {
  23. // 禁用默认行为, 其实就是禁用原<a>标签的点击跳转行为,使用事件中的自定义方法处理
  24. event.preventDefault();
  25. // 跳转到退出事件处理器
  26. window.location.assign('handle01.php?action=logout01');
  27. }
  28. });
  29. </script>
  30. </html>

演示效果

2.login01.php
<?php
// 判断是否在登录状态
if(isset($_COOKIE[‘user’])) $user = unserialize ($_COOKIE[‘user’]);
?>

  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. <link rel="stylesheet" type="text/css" href="../css/index01.css">
  8. </head>
  9. <body>
  10. <nav>
  11. <a href="index01.php">我的论坛</a>
  12. <?php if(isset($user)): ?>
  13. <a href="" id="logout01"><span style="color:red"><?php echo $user['name']?></span>&nbsp;&nbsp;退出</a>
  14. <?php else: ?>
  15. <a href="login01.php">登录</a>
  16. <?php endif ?>
  17. </nav>
  18. </body>
  19. <script>
  20. // 为退出按钮创建事件监听器
  21. document.querySelector('#logout01').addEventListener('click', function(event) {
  22. if (confirm('是否退出')) {
  23. // 禁用默认行为, 其实就是禁用原<a>标签的点击跳转行为,使用事件中的自定义方法处理
  24. event.preventDefault();
  25. // 跳转到退出事件处理器
  26. window.location.assign('handle01.php?action=logout01');
  27. }
  28. });
  29. </script>
  30. </html>

演示效果

3.register01.php
<?php
// 判断是否在登录状态
if(isset($_COOKIE[‘user’])) $user = unserialize ($_COOKIE[‘user’]);
?>

  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. <link rel="stylesheet" type="text/css" href="../css/index01.css">
  8. </head>
  9. <body>
  10. <nav>
  11. <a href="index01.php">我的论坛</a>
  12. <?php if(isset($user)): ?>
  13. <a href="" id="logout01"><span style="color:red"><?php echo $user['name']?></span>&nbsp;&nbsp;退出</a>
  14. <?php else: ?>
  15. <a href="login01.php">登录</a>
  16. <?php endif ?>
  17. </nav>
  18. </body>
  19. <script>
  20. // 为退出按钮创建事件监听器
  21. document.querySelector('#logout01').addEventListener('click', function(event) {
  22. if (confirm('是否退出')) {
  23. // 禁用默认行为, 其实就是禁用原<a>标签的点击跳转行为,使用事件中的自定义方法处理
  24. event.preventDefault();
  25. // 跳转到退出事件处理器
  26. window.location.assign('handle01.php?action=logout01');
  27. }
  28. });
  29. </script>
  30. </html>

演示效果

4.handle01.php
<?php
// 查询用户表中的数据
$pdo = new PDO(‘mysql:host=localhost;dbname=exe0507’,’exe’,’exE123’);
$stmt = $pdo->prepare(‘SELECT * from user1‘);
$stmt->execute();
$heros = $stmt->fetchAll(PDO::FETCH_ASSOC);

  1. // 处理用户登录与注册
  2. $action = $_GET['action'];
  3. switch (strtolower($action)){
  4. // 正常登录
  5. case 'login01':
  6. // 判断请求是否合法
  7. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  8. // 获取需要验证的数据
  9. $email = $_POST['email'];
  10. $password = sha1($_POST['password']);
  11. $results = array_filter($heros, function($user) use ($email, $password) {
  12. return $user['email'] === $email && $user['password'] === $password;
  13. });
  14. if (count($results) === 1) {
  15. setcookie('user', serialize(array_pop($results)));
  16. exit('<script>alert("验证通过");location.href="index01.php"</script>');
  17. } else {
  18. exit('<script>alert("邮箱或密码错误,或者还没有帐号");location.href="login01.php";</script>');
  19. }
  20. } else {
  21. die('请求类型错误');
  22. }
  23. break;
  24. // 退出
  25. case 'logout01':
  26. if (isset($_COOKIE['user'])) {
  27. setcookie('user', null , time()-3600);
  28. exit('<script>alert("退出成功");location.assign("index01.php")</script>');
  29. }
  30. break;
  31. // 用户注册
  32. case 'register01':
  33. // 1. 获取到所有新用户数据
  34. $name = $_POST['name'];
  35. $email = $_POST['email'];
  36. $password = sha1($_POST['ps1']);
  37. $reg_time = time();
  38. // 2. 将新用户插入到表中
  39. $sql = "INSERT `user1` SET `name`='{$name}',`email`='{$email}',`password`='{$password}',`reg_time`={$reg_time} ";
  40. $stmt = $pdo->prepare($sql);
  41. $stmt->execute();
  42. if ($stmt->rowCount() === 1) exit('<script>alert("注册成功");location.assign("login01.php")</script>');
  43. else exit('<script>alert("注册失败");location.assign("login01.php")</script>');
  44. break;
  45. // 未定义
  46. default:
  47. exit('未定义操作');
  48. }

演示效果







session会话

1.index01.php
<?php
// 开启会话
session_start();

  1. // 判断是否在登录状态
  2. if(isset($_SESSION['user'])) $user = unserialize ($_SESSION['user']);
  3. ?>
  4. <!DOCTYPE html>
  5. <html lang="en">
  6. <head>
  7. <meta charset="UTF-8">
  8. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  9. <title>首页</title>
  10. <link rel="stylesheet" type="text/css" href="../css/index01.css">
  11. </head>
  12. <body>
  13. <nav>
  14. <a href="index01.php">我的论坛</a>
  15. <?php if(isset($user)): ?>
  16. <a href="" id="logout01"><span style="color:red"><?php echo $user['name']?></span>&nbsp;&nbsp;退出</a>
  17. <?php else: ?>
  18. <a href="login01.php">登录</a>
  19. <?php endif ?>
  20. </nav>
  21. </body>
  22. <script>
  23. // 为退出按钮创建事件监听器
  24. document.querySelector('#logout01').addEventListener('click', function(event) {
  25. if (confirm('是否退出')) {
  26. // 禁用默认行为, 其实就是禁用原<a>标签的点击跳转行为,使用事件中的自定义方法处理
  27. event.preventDefault();
  28. // 跳转到退出事件处理器
  29. window.location.assign('handle01.php?action=logout01');
  30. }
  31. });
  32. </script>
  33. </html>

2.login01.php
<?php
// 开启会话
session_start();

  1. // 判断是否在登录状态
  2. if(isset($_SESSION['user']))
  3. exit('<script>alert("请不要重复登录");location.href="index01.php";</script>');
  4. ?>
  5. <!DOCTYPE html>
  6. <html lang="en">
  7. <head>
  8. <meta charset="UTF-8">
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  10. <link rel="stylesheet" type="text/css" href="../css/login01.css">
  11. <title>用户登录</title>
  12. </head>
  13. <body>
  14. <h3>用户登录</h3>
  15. <form action="handle01.php?action=login01" method="post">
  16. <div>
  17. <label for="">邮箱:</label>
  18. <input type="email" name="email" id="email" >
  19. </div>
  20. <div>
  21. <label for="">密码:</label>
  22. <input type="password" name="password" id="password" pla >
  23. </div>
  24. <div>
  25. <button type="submit">提交</button>
  26. </div>
  27. </form>
  28. <a href="register01.php">没注册的请点击这里注册吧</a>
  29. </body>
  30. </html>

3.handle01.php
<?php
// 开启会话
session_start();

  1. // 查询用户表中的数据
  2. $pdo = new PDO('mysql:host=localhost;dbname=exe0507','exe','exE123');
  3. $stmt = $pdo->prepare('SELECT * from `user1`');
  4. $stmt->execute();
  5. $heros = $stmt->fetchAll(PDO::FETCH_ASSOC);
  6. // 处理用户登录与注册
  7. $action = $_GET['action'];
  8. switch (strtolower($action)){
  9. // 正常登录
  10. case 'login01':
  11. // 判断请求是否合法
  12. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  13. // 获取需要验证的数据
  14. $email = $_POST['email'];
  15. $password = sha1($_POST['password']);
  16. $results = array_filter($heros, function($user) use ($email, $password) {
  17. return $user['email'] === $email && $user['password'] === $password;
  18. });
  19. if (count($results) === 1) {
  20. $_SESSION['user'] = serialize(array_pop($results));
  21. exit('<script>alert("验证通过");location.href="index01.php"</script>');
  22. } else {
  23. exit('<script>alert("邮箱或密码错误,或者还没有帐号");location.href="login01.php";</script>');
  24. }
  25. } else {
  26. die('请求类型错误');
  27. }
  28. break;
  29. // 退出
  30. case 'logout01':
  31. if (isset($_SESSION['user'])) {
  32. session_destroy();
  33. exit('<script>alert("退出成功");location.assign("index01.php")</script>');
  34. }
  35. break;
  36. // 用户注册
  37. case 'register01':
  38. // 1. 获取到所有新用户数据
  39. $name = $_POST['name'];
  40. $email = $_POST['email'];
  41. $password = sha1($_POST['ps1']);
  42. $reg_time = time();
  43. // 2. 将新用户插入到表中
  44. $sql = "INSERT `user1` SET `name`='{$name}',`email`='{$email}',`password`='{$password}',`reg_time`={$reg_time} ";
  45. $stmt = $pdo->prepare($sql);
  46. $stmt->execute();
  47. if ($stmt->rowCount() === 1) exit('<script>alert("注册成功");location.assign("login01.php")</script>');
  48. else exit('<script>alert("注册失败");location.assign("login01.php")</script>');
  49. break;
  50. // 未定义
  51. default:
  52. exit('未定义操作');
  53. }

register01.php没改动就不贴出代码了。

演示效果








总结:
1.第一次尝试前后台数据传输,心情激动,期待顺利通过!
理想与现实的差别:经过一星期的仿照编码和查错,今天才通过cookie会话整个流程。期间出现编码写错、数据库password选择sha1加密输入出错、数据库重装、phpstudy重装等种种折腾。
2.session会话于cookie会话的区别:
—首先要启动会话session_start();
—变量$_COOKIE改为$_SESSION
—setcookie改为$_SESSION[..]=
—setcookie(‘user’, null , time()-3600);改为session_destroy();
—所以只改动了index01.php、login01.php、handle01.php三个文件。

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