博客列表 >登录与验证,新用户注册功能(cookie 与 session)

登录与验证,新用户注册功能(cookie 与 session)

我是郭富城
我是郭富城原创
2020年05月09日 16:07:50950浏览

登录与验证,新用户注册功能(cookie 与 session)

1.连接数据库用户表

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

3.处理用户注册登录脚本(SESSION)

  1. <?php
  2. session_start();
  3. //处理脚本
  4. $pdo = new PDO('mysql:host=localhost;dbname=php', 'root', 'root123456');
  5. // var_dump($pdo);
  6. // die;
  7. $sql = 'SELECT * FROM `user` ';
  8. $stmt = $pdo->prepare($sql);
  9. $stmt->execute();
  10. $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
  11. // print_r($users);
  12. //处理用户登录和注册
  13. $action = $_GET['action'];
  14. switch (strtolower($action)) {
  15. // 登录
  16. case 'login':
  17. //判断请求是否合法
  18. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  19. //获取需要验证的数据
  20. $email = $_POST['email'];
  21. $password = sha1($_POST['password']);
  22. // echo $email, $password;
  23. // die;
  24. $results = array_filter($users, function ($user) use ($email, $password) {
  25. return $user['email'] === $email && $user['password'] === $password;
  26. });
  27. // print_r($results);
  28. if (count($results) === 1) {
  29. // setcookie('user', serialize(array_pop($results)));
  30. $_SESSION['user'] = serialize(array_pop($results));
  31. exit('<script>alert("验证通过");location.href="index.php"</script>');
  32. } else {
  33. exit('<script>alert("验证失败");location.href="login.php"</script>');
  34. }
  35. } else {
  36. die('请求类型错误');
  37. }
  38. break;
  39. //退出
  40. case 'logout':
  41. if (isset($_SESSION['user'])) {
  42. // setcookie('user', null, time() - 3600);
  43. session_destroy();
  44. exit('<script>alert("退出成功");location.assign("index.php")</script>');
  45. }
  46. break;
  47. //注册
  48. case 'reg';
  49. //获取新用户信息
  50. $name = $_POST['name'];
  51. $email = $_POST['email'];
  52. $password = sha1($_POST['password1']);
  53. $time = time();
  54. // echo $name, $email, $password, $time;
  55. // die;
  56. //2.写入新用户信息
  57. $sql = "INSERT `user` SET `name`='{$name}',`email`='{$email}',`password`='{$password}',`time`={$time}";
  58. $stmt = $pdo->prepare($sql);
  59. $stmt->execute();
  60. if ($stmt->rowCount() === 1) exit('<script>alert("注册成功");location.assign("index.php")</script>');
  61. else exit('<script>alert("注册失败");location.assign("index.php")</script>');
  62. break;
  63. default:
  64. echo ('未定义操作');
  65. }

4. 前端代码分享

4.1 首页

  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="en">
  9. <head>
  10. <meta charset="UTF-8" />
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  12. <title>首页</title>
  13. <link rel="stylesheet" href="../css/index.css" />
  14. </head>
  15. <body>
  16. <nav>
  17. <div><a href="">logo</a></div>
  18. <span>
  19. <a href="index.php">官网首页</a>
  20. <a href="index.php">HOME</a>
  21. </span>
  22. <span>
  23. <a href="">活动中心</a>
  24. <a href="">CONTENTS</a>
  25. </span>
  26. <span>
  27. <a href="">新闻公告</a>
  28. <a href="">NEWS</a>
  29. </span>
  30. <span>
  31. <a href="">排行榜</a>
  32. <a href="">LIST</a>
  33. </span>
  34. <span>
  35. <a href="">兑换中心</a>
  36. <a href="">EXCHANGE</a>
  37. </span>
  38. <span>
  39. <a href="">博客之家</a>
  40. <a href="">BLOG</a>
  41. </span>
  42. <span>
  43. <a href="">发布任务</a>
  44. <a href="">TASK</a>
  45. </span>
  46. <span>
  47. <a href="">投诉建议</a>
  48. <a href="">ADVICE</a>
  49. </span>
  50. <span>
  51. <a href="">个人中心</a>
  52. <a href="">CENTER</a>
  53. </span>
  54. <div>
  55. <div>
  56. <?php if (isset($user)) : ?>
  57. <span>
  58. <a href="" id="logout"><span style="color: red"><?php echo $user['name'] ?></span> 退出</>
  59. </span>
  60. <?php else : ?>
  61. <span>
  62. <a href="reg.php">注册</a>
  63. <a href="login.php">登录</a>
  64. </span>
  65. <?php endif ?>
  66. <a href="">HI,欢迎光临</a>
  67. </div>
  68. </div>
  69. </nav>
  70. </body>
  71. <script>
  72. // 为退出按钮创建事件监听器
  73. document.querySelector('#logout').addEventListener('click', function(event) {
  74. if (confirm('是否退出')) {
  75. // 禁用默认行为, 其实就是禁用原<a>标签的点击跳转行为,使用事件中的自定义方法处理
  76. event.preventDefault();
  77. // 跳转到退出事件处理器
  78. window.location.assign('handle.php?action=logout');
  79. }
  80. });
  81. </script>
  82. </html>

4.2 注册页面

  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" href="../css/style.css" />
  8. </head>
  9. <body>
  10. <div>
  11. <div>
  12. <div>账号注册</div>
  13. <form action="handle.php?action=reg" method="POST" onsubmit="return compare()">
  14. <section>
  15. <label for="email">电子邮箱:</label>
  16. <input type="email" name="email" id="email" required autofocus placeholder="请输入邮箱" />
  17. </section>
  18. <section>
  19. <label for="name">用户昵称:</label>
  20. <input type="text" name="name" id="name" required autofocus placeholder="请输入用户名称" />
  21. </section>
  22. <section>
  23. <label for="password1">输入密码:</label>
  24. <input type="password" name="password1" id="password1" required placeholder="请输入密码" />
  25. </section>
  26. <section>
  27. <label for="password2">重复密码:</label>
  28. <input type="password" name="password2" id="password2" required placeholder="请重复密码" />
  29. </section>
  30. <section>
  31. <button>提交</button><span id="tips" style="color: red"></span>
  32. </section>
  33. </form>
  34. <p>已有账号?请点击<a href="login.php">登录</a></p>
  35. </div>
  36. </div>
  37. <script>
  38. // 验证二次密码是否相等?
  39. function compare() {
  40. if (document.forms[0].password1.value.trim() !== document.forms[0].password2.value.trim()) {
  41. document.querySelector('#tips').innerText = '二次密码不相等';
  42. return false;
  43. }
  44. }
  45. </script>
  46. </body>
  47. </html>

4.3 登陆页面

  1. <?php
  2. session_start();
  3. //防止重复登陆
  4. if (isset($_SESSION['user'])) exit('<script>alert("你已经登陆了");location.href="index.php";</script>');
  5. // print_r($user);
  6. ?>
  7. <!DOCTYPE html>
  8. <html lang="en">
  9. <head>
  10. <meta charset="UTF-8" />
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  12. <title>用户登录</title>
  13. <link rel="stylesheet" href="../css/style.css" />
  14. </head>
  15. <body>
  16. <div>
  17. <div>
  18. <div>账号登录</div>
  19. <form action="handle.php?action=login" method="POST">
  20. <section>
  21. <label for="email">电子邮箱:</label>
  22. <input type="email" name="email" id="email" required autofocus placeholder="请输入邮箱" />
  23. </section>
  24. <section>
  25. <label for="password">输入密码:</label>
  26. <input type="password" name="password" id="password" required placeholder="请输入密码" />
  27. </section>
  28. <button>提交</button>
  29. </form>
  30. <p>还没账号?请点击<a href="reg.php">注册</a></p>
  31. </div>
  32. </div>
  33. </body>
  34. </html>

5. css 样式分享

5.1 首页 index.css

  1. body {
  2. min-width: 1200px;
  3. }
  4. * {
  5. padding: 0;
  6. margin: 0;
  7. }
  8. a {
  9. text-decoration: none;
  10. color: #cdcde1;
  11. }
  12. nav {
  13. display: flex;
  14. height: 83px;
  15. align-items: center;
  16. padding: 0 20px;
  17. justify-content: space-between;
  18. background-color: #000;
  19. }
  20. nav > div:first-of-type {
  21. width: 220px;
  22. text-align: center;
  23. }
  24. nav > span {
  25. width: 111px;
  26. display: flex;
  27. flex-direction: column;
  28. align-items: center;
  29. height: 83px;
  30. justify-content: center;
  31. }
  32. nav > span > a {
  33. padding: 5px;
  34. }
  35. nav > div:last-of-type > div {
  36. display: flex;
  37. height: 83px;
  38. flex-direction: column;
  39. align-items: center;
  40. justify-content: space-evenly;
  41. }

5.2 登陆页面注册页面样式 style.css

  1. body {
  2. min-width: 1200px;
  3. margin: 0;
  4. background: url(../bg.png) no-repeat;
  5. height: 100%;
  6. width: 100%;
  7. overflow: hidden;
  8. background-size: 100%;
  9. display: flex;
  10. justify-content: center;
  11. /* align-items: center; */
  12. }
  13. body > div {
  14. background: #fff;
  15. width: 910px;
  16. /* height: 968px; */
  17. border-radius: 50px;
  18. margin: 10px;
  19. }
  20. h3 {
  21. text-align: center;
  22. }
  23. body > div > div {
  24. height: 300px;
  25. display: flex;
  26. flex-direction: column;
  27. align-items: center;
  28. justify-content: space-evenly;
  29. }
  30. body > div > div > div {
  31. font-size: 2.5rem;
  32. color: #e9cbac;
  33. font-weight: bolder;
  34. }

6. 进阶操作

  • 验证码
  1. //创建验证码
  2. public function createCode(){
  3. $strlen = strlen($this->string)-1;
  4. for ($i=0; $i < $this->codeNum; $i++) {
  5. $this->code .= $this->string[mt_rand(0,$strlen)]; //从字符集中随机取出四个字符拼接
  6. }
  7. $_SESSION['code'] = $this->code; //加入 session 中
  8. //计算每个字符间距
  9. $diff = $this->width/$this->codeNum;
  10. for ($i=0; $i < $this->codeNum; $i++) {
  11. //为每个字符生成颜色(使用深色)
  12. $txtColor = imagecolorallocate($this->img,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
  13. //写入图像
  14. imagettftext($this->img, $this->fontSize, mt_rand(-30,30), $diff*$i+mt_rand(3,8), mt_rand(20,$this->height-10), $txtColor, $this->font, $this->code[$i]);
  15. }
  16. }
  • 验证手机短信:正在学习如何使用别人的接口

7.总结

本节课学习了绘画控制的基本知识,简单地说,Cookie 是通过在客户端中记录信息而确定用户身份;Session 是通过在服务器端记录信息而确定用户身份。

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