博客列表 >使用cookie与session来实现用户登录、注册、验证

使用cookie与session来实现用户登录、注册、验证

昔年
昔年原创
2020年05月12日 19:56:222458浏览

cookie实现用户登录、注册、验证

1.用户登录页面

1.1html代码

  1. <?php
  2. //判断是否已经登录
  3. if (isset($_COOKIE['user']))
  4. exit('<script>alert("请不要重复登录");location.href="index.php"</script>');
  5. ?>
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8">
  10. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  11. <link rel="stylesheet" type="text/css" href="../css/style.css">
  12. <title>用户登录</title>
  13. </head>
  14. <body>
  15. <h3>用户登录</h3>
  16. <form action="handle.php?action=login" method="post">
  17. <div>
  18. <label for="email">邮箱:</label>
  19. <input type="email" name="email" id="email" placeholder="email@php.cn" autofocus required>
  20. </div>
  21. <div>
  22. <label for="password">密码:</label>
  23. <input type="password" name="password" id="password" placeholder="密码不少于6位" required>
  24. </div>
  25. <div>
  26. <button>点击登录</button>
  27. </div>
  28. </form>
  29. <a href="register.php">还没有账号,注册一个吧</a>
  30. </body>
  31. </html>

1.2 css代码

  1. body {
  2. display: flex;
  3. flex-direction: column;
  4. text-align: center;
  5. color: #555;
  6. font-weight: 300;
  7. }
  8. body h3 {
  9. font-weight: 300;
  10. font-size: 20px;
  11. margin-bottom: 10px;
  12. }
  13. body form {
  14. width: 250px;
  15. padding: 20px;
  16. box-sizing: border-box;
  17. margin: auto;
  18. border-radius: 5px;
  19. box-shadow: 0 0 5px #aaa;
  20. }
  21. body form > div {
  22. height: 36px;
  23. display: flex;
  24. justify-content: space-between;
  25. align-items: center;
  26. }
  27. body form div > label {
  28. width: 50px;
  29. }
  30. body form div:last-of-type {
  31. display: flex;
  32. justify-content: center;
  33. }
  34. body form input:hover {
  35. box-shadow: 0 0 5px #aaa;
  36. }
  37. body form button {
  38. flex: auto;
  39. height: 30px;
  40. background-color: green;
  41. color: white;
  42. border: none;
  43. outline: none;
  44. }
  45. body form button:hover {
  46. background-color: lightcoral;
  47. cursor: pointer;
  48. box-shadow: 0 0 5px #aaa;
  49. }
  50. body a {
  51. color: #888;
  52. text-decoration: none;
  53. margin-top: 15px;
  54. }
  55. body a:hover {
  56. color: lightcoral;
  57. font-weight: bold;
  58. }

2.用户注册

2.1html代码

  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" href="../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@php.cn" 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></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>

2.2 css代码

同上面的用户登录

3 登录成功

3.1用户首页

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

3.1 css样式

  1. nav {
  2. height: 40px;
  3. background-color: deepskyblue;
  4. padding: 0 20px;
  5. display: flex;
  6. justify-content: space-between;
  7. align-items: center;
  8. }
  9. nav > a {
  10. color: white;
  11. text-decoration: none;
  12. }

4 控制部分

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

总结:用户登录注册通过把控制处理代码统一写在一个模块,这个就是MVC中的控制器,通过控制来处理这些逻辑,用户是看不到这个部分代码的
session实现只需要把用到cookie的部分替换成session,然后在代码前面加上session_start()就可以实现了

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