Blogger Information
Blog 38
fans 0
comment 0
visits 18535
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
完成新用户注册的过程 ,并详细会话的完整流程,以及注意事项
Blackeye
Original
727 people have browsed it

1
2

index.php

  1. <?php
  2. //开启会话获取session
  3. session_start();
  4. // 判断用户是否已经登录?
  5. if (isset($_SESSION['user'])) $user = unserialize($_SESSION['user']);
  6. ?>
  7. <!DOCTYPE html>
  8. <html lang="zh-CN">
  9. <head>
  10. <meta charset="UTF-8">
  11. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  12. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  13. <title>Document</title>
  14. </head>
  15. <body>
  16. <nav>
  17. <!-- 判断用户是否已经登录对应的显示 -->
  18. <?php if (isset($user)) : ?>
  19. <a href="" id="logout">退出</a>
  20. <?php else : ?>
  21. <a href="login.php">登录</a>
  22. <?php endif ?>
  23. </nav>
  24. <script>
  25. document.querySelector('#logout').addEventListener('click', function(event) {
  26. if (confirm('是否退出?')) {
  27. // 禁用默认跳转行为
  28. event.preventDefault();
  29. // 跳转到处理器
  30. location.assign('handle.php?action=logout');
  31. }
  32. });
  33. </script>
  34. </body>
  35. </html>

login.php

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <?php
  4. //开启session会话
  5. session_start();
  6. // 判断用户是否已经登录?
  7. if (isset($_SESSION['user'])) echo '<script>alert("不要重复登录");locatoin.href="index.php"</script>';
  8. ?>
  9. <head>
  10. <meta charset="UTF-8">
  11. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  12. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  13. <title>用户登录表单</title>
  14. </head>
  15. <body>
  16. <form action="handle.php?action=login" method="POST">
  17. <fieldset style="display: inline-block;background:lightcyan">
  18. <legend>用户登录</legend>
  19. <p>
  20. <input type="email" name="email" placeholder="user@email.com" require>
  21. </p>
  22. <p>
  23. <input type="password" name="password" placeholder="不少于6位" require>
  24. </p>
  25. <p>
  26. <button>提交</button>
  27. </p>
  28. </fieldset>
  29. <a href="register.php">如果没有帐号,请先注册</a>
  30. </form>
  31. </body>
  32. </html>

handle.php

  1. <?php
  2. // 开启会话
  3. session_start();
  4. // 根据用户的不同请求,执行不同的操作
  5. // 比如:登录 , 注册, 退出
  6. // 连接数据并获取用户表中的数据
  7. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  8. $stmt = $db->prepare('SELECT * FROM `user`');
  9. $stmt->execute();
  10. $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
  11. // print_r($users);
  12. $action = $_GET['action'];
  13. switch (strtolower($action)) {
  14. // 登录
  15. case 'login':
  16. //要保证数据是通用POST请求发送的
  17. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  18. // 先拿到登录数据
  19. extract($_POST);
  20. // $email = $_POST['email'];
  21. // $password = sha1($_POST['password']);
  22. // $result 是数组
  23. $result = array_filter($users, function ($user) use ($email, $password) {
  24. return $user['email'] === $email && $user['password'] === md5($password);
  25. });
  26. if (count($result) === 1) {
  27. // 验证成功,将用户信息写到SESSION
  28. // print_r(serialize(array_pop($result)));
  29. // $a = serialize(array_pop($result));
  30. // print_r(unserialize($a));
  31. // 将用户信息序列化之后保存到SESSION中
  32. $_SESSION['user'] = serialize(array_pop($result));
  33. exit('<script>alert("验证通过");location.href="index.php"</script>');
  34. } else {
  35. exit('<script>alert("邮箱或密码错误");location.href="index.php"</script>');
  36. }
  37. } else {
  38. die('请求错误');
  39. }
  40. break;
  41. // 退出
  42. case 'logout':
  43. if (isset($_SESSION['user'])) {
  44. session_destroy();
  45. exit('<script>alert("退出成功");location.assign("index.php")</script>');
  46. }
  47. break;
  48. // 注册
  49. case 'register':
  50. // 1. 获取到新用户的信息
  51. $name = $_POST['name'];
  52. $email = $_POST['email'];
  53. $password = md5($_POST['psw1']);
  54. $register_time = time();
  55. // 2. 将新用户添加到表中
  56. $sql = 'INSERT user SET name=?,email=?,password=?,register_time=?';
  57. $stmt = $db->prepare($sql);
  58. $stmt->execute([$name, $email, $password, $register_time]);
  59. if ($stmt->rowCount() === 1) {
  60. echo '<script>alert("注册成功");locaton.href="index.php"</script>';
  61. } else {
  62. exit('<script>alert("注册失败");locaton.href="index.php"</script>');
  63. }
  64. break;
  65. }

register.php

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <?php
  4. session_start();
  5. // 判断用户是否已经登录?
  6. if (isset($_SESSION['user'])) echo '<script>alert("不要重复登录");locatoin.href="index.php"</script>';
  7. ?>
  8. <head>
  9. <meta charset="UTF-8">
  10. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  12. <title>用户注册表单</title>
  13. </head>
  14. <body>
  15. <form action="handle.php?action=login" method="POST">
  16. <fieldset style="display: inline-block;background:lightcyan">
  17. <legend>用户注册</legend>
  18. <p>
  19. <input type="email" name="email" placeholder="user@email.com" require>
  20. </p>
  21. <p>
  22. <input type="password" name="password" id="pwd_o" placeholder="不少于6位" require>
  23. </p>
  24. <p>
  25. <input type="password" name="password" id="pwd_t" placeholder="二次必须一致" require>
  26. </p>
  27. <!-- 二次密码是否一致用JS进行验证就可以了 -->
  28. <p>
  29. <button>提交</button>
  30. </p>
  31. </fieldset>
  32. </form>
  33. </body>
  34. <script>
  35. // 获取第一次密码input dom
  36. const pwd_o = document.querySelector('#pwd_o');
  37. // 获取第二次密码input dom
  38. const pwd_t = document.querySelector('#pwd_t');
  39. // 第二次密码失去焦点事件
  40. pwd_t.addEventListener('blur',function(){
  41. if(pwd_o.value!==pwd_t.value){
  42. pwd_t.value="";
  43. pwd_t.placeholder="两次密码不一致!!!";
  44. }
  45. });
  46. </script>
  47. </html>
Correcting teacher:PHPzPHPz

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!