Blogger Information
Blog 26
fans 0
comment 0
visits 18140
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
COOKIE与SESSION实现用户登录、注册、验证
雪~人胖胖
Original
1521 people have browsed it

1.cookie与session

发送cookie

  1. setcookie ( string $name [, string $value = "" [, int $expire = 0 [, string $path = ""
  2. 设置setcookie('user',serialize(array_pop($result)),time()+3600);
  3. 删除setcookie('user',null,time()-60);

session用法

  1. //开始会话
  2. session_start();
  3. //销毁会话
  4. session_destroy();

用户首页

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <?php
  4. if (isset($_COOKIE['user'])) $user=unserialize($_COOKIE['user']);
  5. ?>
  6. <?php
  7. //session用法
  8. //session_start();
  9. //if (isset($_SESSION['user'])) $user=unserialize($_SESSION['user']);
  10. ?>
  11. <head>
  12. <meta charset="UTF-8">
  13. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  14. <title>首页</title>
  15. <link rel="stylesheet" href="css/style.css">
  16. </head>
  17. <body>
  18. <nav>
  19. <a href="">我的首页</a>
  20. <?php if(isset($_COOKIE['user'])):?>
  21. //session的用法
  22. //<?php if(isset($_SESSION['user'])):?>
  23. <a href="" id=logout><span style="color: red"><?php echo $user['username'] ?></span>退出</a>
  24. <?php else: ?>
  25. <a href="login.php">登录</a>
  26. <?php endif ?>
  27. </nav>
  28. </body>
  29. <script>
  30. //点击退出事件
  31. document.querySelector('#logout').addEventListener('click', function(event) {
  32. if (confirm('是否退出')) {
  33. event.preventDefault();
  34. window.location.assign('handle.php?action=logout');
  35. }
  36. });
  37. </script>
  38. </html>

登录页

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <?php
  4. //使用session
  5. //session_start();
  6. //if(isset($_SESSION['user']))
  7. if(isset($_COOKIE['user']))
  8. exit('<script>alert("请不要重复登录");location.href="index.php";</script>');
  9. ?>
  10. <head>
  11. <meta charset="UTF-8">
  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>
  18. <legend>欢迎登录</legend>
  19. <div>
  20. <label for="email">邮箱:</label>
  21. <input type="email" name="email" id="email" autofocus placeholder="admin@php.com" required>
  22. </div>
  23. <div>
  24. <label for="password">密码:</label>
  25. <input type="password" name="password" id="password" placeholder="不少于4位不多于10位" required>
  26. </div>
  27. <div><button>登录</button><button><a href="register.php">去注册</a></button></div>
  28. </fieldset>
  29. </form>
  30. </body>
  31. </html>

注册页

  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. </head>
  8. <body>
  9. <form action="handle.php?action=register" method="POST">
  10. <fieldset>
  11. <legend>欢迎注册</legend>
  12. <div>
  13. <label for="username">用户名:</label>
  14. <input type="text" name="username" id="username" placeholder="不少于5位不多于10位" autofocus required>
  15. </div>
  16. <div>
  17. <label for="email">邮箱:</label>
  18. <input type="email" name="email" id="email" autofocus placeholder="admin@php.com" required>
  19. </div>
  20. <div>
  21. <label for="password1">密码:</label>
  22. <input type="password" name="password1" id="password1" placeholder="不少于4位不多于10位" required>
  23. </div>
  24. <div>
  25. <label for="password2">重复密码:</label>
  26. <input type="password" name="password2" id="password2" placeholder="不少于4位不多于10位" required>
  27. </div>
  28. <div><button>注册</button></div>
  29. </fieldset>
  30. </form>
  31. </body>
  32. </html>

控制器

  1. <?php
  2. //数据库查询
  3. $pdo = new PDO('mysql:host=localhost;dbname=phpedu','root','root');
  4. $sql = 'SELECT `*` FROM `users`';
  5. $stmt = $pdo->prepare($sql);
  6. $stmt->execute();
  7. $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
  8. //session_start();
  9. //////////////////////////////////////////
  10. $action = $_GET['action'];
  11. switch(strtolower($action)){
  12. case 'login':
  13. //判断请求是否合法
  14. if ($_SERVER['REQUEST_METHOD'] === 'POST'){
  15. $email = $_POST['email'];
  16. $password = sha1($_POST['password']);
  17. $result = array_filter($users,function($user) use($email,$password){
  18. return $user['email'] === $email && $user['password'] === $password;
  19. });
  20. if(count($result) === 1){
  21. //$_SESSION['user']=serialize(array_pop($result));
  22. setcookie('user',serialize(array_pop($result)),time()+3600);
  23. exit('<script>alert("验证通过");location.href="index.php"</script>');
  24. }else{
  25. exit('<script>alert("验证不通过");location.href="login.php"</script>');
  26. }
  27. }else{
  28. die('请求非法');
  29. }
  30. break;
  31. case 'logout':
  32. //if(isset($_SESSION['user'])){
  33. //session_destroy();
  34. if(isset($_COOKIE['user'])){
  35. setcookie('user',null,time()-60);
  36. exit('<script>alert("退出成功");location.assign("index.php")</script>');
  37. }
  38. break;
  39. case 'register':
  40. //获取数据
  41. $username =$_POST['username'];
  42. $email =$_POST['email'];
  43. $password =sha1($_POST['password1']);
  44. $register_time =time();
  45. //验证数据库有无该邮箱
  46. $stmt = $pdo->prepare("SELECT `email` FROM `users` WHERE `email`='{$email}'");
  47. $stmt->execute();
  48. $db_email = $stmt->fetchAll(PDO::FETCH_ASSOC);
  49. if(empty($db_email)===true){
  50. $sql = "INSERT `users` SET `username`='{$username}',`email`='{$email}',`password`='{$password}',`register_time`='{$register_time}'";
  51. $stmt=$pdo->prepare($sql);
  52. $stmt->execute();
  53. if ($stmt->rowCount()===1){
  54. exit('<script>alert("注册成功");location.assign("login.php")</script>');
  55. }else{
  56. exit('<script>alert("注册失败");location.assign("register.php")</script>');
  57. }
  58. }else{
  59. exit('<script>alert("邮箱已存在");location.assign("register.php")</script>');
  60. }
  61. break;
  62. default:
  63. exit('未定义错误');
  64. }

感想

刚开始写的时候是在看了一遍视频以后,犯了很多的错误,各种漏写,之后一步一步验证错误,在结合老师的视频,又理了一遍,用户登录注册通过把控制处理代码统一写在一个模块,通过控制器来处理这些逻辑。

Correcting teacher:天蓬老师天蓬老师

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!