ASCII码 ASCII码

新用户注册、登录、退出

发布于:2022-05-19 09:45:04  栏目:技术文档

index.php

  1. <?php
  2. session_start();
  3. // 判断用户是否已经登录?
  4. if (isset($_SESSION['user'])) $user = unserialize($_SESSION['user']);
  5. // print_r($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. <?php if (isset($user)) : ?>
  18. <a href="" id="logout">退出</a>
  19. <?php else : ?>
  20. <a href="login.php">登录</a>
  21. <?php endif ?>
  22. </nav>
  23. <script>
  24. document.querySelector('#logout').addEventListener('click', function(event) {
  25. if (confirm('是否退出?')) {
  26. // 禁用默认跳转行为
  27. event.preventDefault();
  28. // 跳转到处理器
  29. location.assign('handle.php?action=logout');
  30. }
  31. });
  32. </script>
  33. </body>
  34. </html>

login.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" placeholder="不少于6位" require>
  23. </p>
  24. <p>
  25. <button>提交</button>
  26. </p>
  27. </fieldset>
  28. <a href="register.php">如果没有帐号,请先注册</a>
  29. </form>
  30. </body>
  31. </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. } else {
  47. session_destroy();
  48. exit('<script>alert("退出成功");location.assign("index.php")</script>');
  49. }
  50. break;
  51. // 注册
  52. case 'register':
  53. // 1. 获取到新用户的信息
  54. extract($_POST);
  55. $password = md5($password);
  56. $register_time = time();
  57. $name = getSubstr($email, '@');
  58. echo '账号:', $name, '<br>';
  59. echo '邮箱:', $email, '<br>';
  60. echo '密码:', $password, '<br>';
  61. echo '注册时间:', $register_time, '<br>';
  62. echo ('<a href="handle.php?action=logout">退出<a>');
  63. // 2. 将新用户添加到表中
  64. $sql = 'INSERT user SET name=?,email=?,password=?,register_time=?';
  65. $stmt = $db->prepare($sql);
  66. $stmt->execute([$name, $email, $password, $register_time]);
  67. if ($stmt->rowCount() === 1) {
  68. echo '<script>alert("注册成功");locaton.href="index.php"</script>';
  69. } else {
  70. exit('<script>alert("注册失败");locaton.href="index.php"</script>');
  71. }
  72. break;
  73. }
  74. //取文本左边
  75. function getSubstr($str, $rightStr)
  76. {
  77. $right = strpos($str, $rightStr);
  78. return substr($str, 0, $right);
  79. }

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=register" 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" value="tianning@qq.com" require>
  20. </p>
  21. <p>
  22. <input type="password" name="password" placeholder="不少于6位" value="666666" require>
  23. </p>
  24. <p>
  25. <input type="password" name="password" placeholder="二次必须一致" value="666666" require>
  26. </p>
  27. <!-- 二次密码是否一致用JS进行验证就可以了 -->
  28. <p>
  29. <button onclick="verifyPassword(event)">提交</button>
  30. </p>
  31. </fieldset>
  32. </form>
  33. <script>
  34. function verifyPassword(event) {
  35. let input = document.querySelectorAll('input');
  36. if (input[1].value !== input[2].value) {
  37. alert("两次密码不一致,请重新输入");
  38. input[1].value = null;
  39. input[2].value = null;
  40. event.preventDefault();
  41. }
  42. }
  43. </script>
  44. </body>
  45. </html>
相关推荐
阅读 +