博客列表 >php实现注册登录流程操作

php实现注册登录流程操作

kiraseo_wwwkiraercom
kiraseo_wwwkiraercom原创
2022年08月22日 23:12:35559浏览

php实现注册登录流程操作

代码

index.php

  1. <?php
  2. namespace _0822;
  3. session_start();
  4. // 判断是否已登录?
  5. if (isset($_SESSION['user'])) {
  6. $user = unserialize($_SESSION['user']);
  7. }
  8. function get_time($var){
  9. return date('Y-m-d H:i:s',$var);
  10. }
  11. ?>
  12. <!DOCTYPE html>
  13. <html lang="zh-CN">
  14. <head>
  15. <meta charset="UTF-8">
  16. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  17. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <title>首页/入口文件</title>
  19. <link rel="stylesheet" href="/0822/zuoye/login/css/index.css">
  20. </head>
  21. <body>
  22. <nav>
  23. <a href="index.php">我的博客</a>
  24. <?php if (isset($user)) : ?>
  25. <span style="margin-left: 300px"><?=$user['name']?></span>
  26. <a id="logout">退出</a>
  27. <?php else: ?>
  28. <a href="login.php">登录</a>
  29. <?php endif ?>
  30. </nav>
  31. <script>
  32. // 为退出按钮创建事件监听器
  33. document.querySelector('#logout').addEventListener('click', function(event) {
  34. if (confirm('是否退出')) {
  35. // 禁用默认行为, 其实就是禁用原<a>标签的点击跳转行为,使用事件中的自定义方法处理
  36. event.preventDefault();
  37. // 跳转到退出事件处理器
  38. window.location.assign('handle.php?action=logout');
  39. }
  40. });
  41. </script>
  42. <?php
  43. if( isset($user)){
  44. $time = get_time($user['register_time']);
  45. echo "<ul>";
  46. echo "<li>昵称:{$user['name']}</li>";
  47. echo "<li>邮箱:{$user['email']}</li>";
  48. echo "<li>注册时间:$time</li>";
  49. echo "</ul>";
  50. }else{
  51. echo '<a href="register.php">还没有帐号, 等你注册一个吧</a>';
  52. echo '<br>';
  53. echo '<a href="login.php">我有帐号,直接登录</a>';
  54. }
  55. ?>
  56. </body>
  57. </html>

login.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>用户登录</title>
  6. <link rel="stylesheet" type="text/css" href="/0822/zuoye/login/css/style.css">
  7. </head>
  8. <body>
  9. <h3>用户登录</h3>
  10. <form action="handle.php?action=login" method="post">
  11. <div>
  12. <label for="email">邮箱:</label>
  13. <input type="email" name="email" id="email" placeholder="demo@email.com" required autofocus>
  14. </div>
  15. <div>
  16. <label for="password">密码:</label>
  17. <input type="password" name="password" id="password" placeholder="不少于6位" required>
  18. </div>
  19. <div>
  20. <button>提交</button>
  21. </div>
  22. </form>
  23. <a href="register.php">还没有帐号, 注册一个吧</a>
  24. </body>
  25. </html>

register.php

  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. <link rel="stylesheet" type="text/css" href="/0822/zuoye/login/css/style.css">
  7. <title>注册用户</title>
  8. </head>
  9. <body>
  10. <h3>用户注册</h3>
  11. <form action="handle.php?action=register" method="post" onsubmit="return compare()">
  12. <div>
  13. <label for="name">呢称:</label>
  14. <input type="text" name="name" id="name" placeholder="不少于3个字符" required autofocus>
  15. </div>
  16. <div>
  17. <label for="email">邮箱:</label>
  18. <input type="email" name="email" id="email" placeholder="demo@email.com" required>
  19. </div>
  20. <div>
  21. <label for="p1">密码:</label>
  22. <input type="password" name="p1" id="p1" placeholder="不少于6位" required>
  23. </div>
  24. <div>
  25. <label for="p2">重复:</label>
  26. <input type="password" name="p2" id="p2" placeholder="必须与上面一致" required>
  27. </div>
  28. <div>
  29. <button>提交</button><span id="tips" style="color: red"></span>
  30. </div>
  31. </form>
  32. <a href="login.php">我有帐号,直接登录</a>
  33. <script>
  34. // 验证二次密码是否相等?
  35. function compare() {
  36. if (document.forms[0].p1.value.trim() !== document.forms[0].p2.value.trim()) {
  37. document.querySelector('#tips').innerText = '二次密码不相等';
  38. return false;
  39. }
  40. }
  41. </script>
  42. </body>
  43. </html>

handle.php

  1. <?php
  2. namespace _0822;
  3. use PDO;
  4. // 开启会话:必须写在顶部
  5. session_start();
  6. //查询用户表中的数据use表
  7. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  8. $stmt = $db->prepare('SELECT * FROM `users`;');
  9. if ($stmt->execute()) {
  10. $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
  11. } else {
  12. print_r($stmt->errorInfo());
  13. }
  14. // 获取用户操作类型
  15. $action = strtolower($_GET['action']);
  16. switch ($action) {
  17. // 1. 登录
  18. case 'login':
  19. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  20. // 获取登录用户的数据:邮箱和密码
  21. $email= $_POST['email'];
  22. $password= sha1($_POST['password']);
  23. $result = array_filter($users, function ($user) use ($email, $password) {
  24. return $user['email'] === $email && $user['password'] === $password;
  25. });
  26. if (count($result) === 1) {
  27. // 登录成功,写入session
  28. $_SESSION['user'] = serialize(array_pop($result));
  29. exit('<script>alert("验证通过");location.href="index.php"</script>');
  30. }
  31. // echo '请求类型错误'; die;
  32. exit('请求类型错误');
  33. }
  34. // 2. 退出
  35. // no break
  36. case 'logout':
  37. if (isset($_SESSION['user'])) {
  38. session_destroy();
  39. exit('<script>alert("退出成功");location.href="index.php"</script>');
  40. }
  41. // . 注册
  42. // no break
  43. case 'register':
  44. if ($_SERVER['REQUEST_METHOD'] === 'POST'){
  45. // 1. 获取新用户的数据
  46. $email= $_POST['email'];
  47. $name= $_POST['name'];
  48. $password= sha1($_POST['p2']);
  49. $register_time = time();
  50. //防止用户重复提交验证
  51. $result = array_filter($users, function ($user) use ($email, $password) {
  52. return $user['email'] === $email && $user['password'] === $password;
  53. });
  54. if(count($result) === 1){
  55. exit('<script>alert("请勿重复提交数据");location.href="register.php"</script>');
  56. }
  57. // 2. sql
  58. $sql = <<< SQL
  59. INSERT `users`
  60. SET `name`= ?,
  61. `email`= ?,
  62. `password`= ?,
  63. `register_time`= ?;
  64. SQL;
  65. $stmt = $db->prepare($sql);
  66. $data = [$name,$email,$password, $register_time];
  67. if ($stmt->execute($data)) {
  68. if ($stmt->rowCount() > 0) {
  69. // 注册成功之后,让用户自动登录
  70. $sql='SELECT * FROM `users` WHERE `id` = ' . $db->lastInsertId();
  71. $stmt = $db->prepare($sql);
  72. $stmt->execute();
  73. $newUser =$stmt->fetch(PDO::FETCH_ASSOC);
  74. $_SESSION['user'] = serialize($newUser);
  75. exit('<script>alert("注册成功");location.href="index.php"</script>');
  76. } else {
  77. exit('<script>alert("注册失败");location.href="register.php"</script>');
  78. }
  79. } else {
  80. print_r($stmt->errorInfo());
  81. }
  82. }
  83. // no break
  84. default:
  85. exit('参数非法或未定义操作');
  86. }

效果图

1默认未登录状体

2注册成功

3退出相关操作


4注册页面

对重复提交相同信息用户信息提示

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议