博客列表 >用会话控制写登录注册

用会话控制写登录注册

default
default原创
2020年05月11日 17:42:13768浏览

首页代码

  1. <?php
  2. //检测是不是有 cookie 有的话就赋值给$user 没有就是空
  3. isset($_COOKIE['user'])?$user=unserialize($_COOKIE['user']):null;
  4. ?>
  5. <!doctype html>
  6. <html lang="en">
  7. <head>
  8. <meta charset="UTF-8">
  9. <meta name="viewport"
  10. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  11. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  12. <title>爬宠乐园</title>
  13. <style>
  14. *{text-decoration: none;margin: 0;padding: 0;}
  15. body{display: flex;flex-flow: column;font-weight: 800;}
  16. header{ box-sizing: border-box;padding: 10px 30px;width: 100%;display: flex;flex-flow: row nowrap;justify-content: space-between; background: lightseagreen; }
  17. /*.logo*/
  18. header div:nth-of-type(2){color: #ffd012;text-align: center;line-height: 42px }
  19. header div:nth-of-type(3){display: flex;justify-content: space-between;line-height: 42px }
  20. header div:nth-of-type(3) a{color:#ffd012; }
  21. </style>
  22. </head>
  23. <body>
  24. <header>
  25. <div class="logo"><h1>爬宠乐园</h1></div>
  26. <div>一个程序员的爱好社区</div>
  27. <div>
  28. <?php if (isset($user)):?>
  29. <a href=""><?php echo $user['name']?></a>&nbsp;&nbsp;
  30. <a href="" id="logout">退出</a>
  31. <?php else:?>
  32. <a href="login.php">登陆</a>&nbsp;&nbsp;
  33. <a href="register.php">注册</a>
  34. <?php endif;?>
  35. </div>
  36. </header>
  37. <script>
  38. document.querySelector('#logout').addEventListener('click',function (event) {
  39. if (confirm('确认退出吗?')) {
  40. // 禁用默认行为, 其实就是禁用原<a>标签的点击跳转行为,使用事件中的自定义方法处理
  41. event.preventDefault();
  42. // 跳转到退出事件处理器
  43. window.location.assign('handle.php?action=logout');
  44. }
  45. })
  46. </script>
  47. </body>
  48. </html>

登陆代码

  1. <?php
  2. // 判断是否已登录
  3. if (isset($_COOKIE['user']))
  4. exit('<script>alert("请不要重复登录");location.href="index.php";</script>');
  5. ?>
  6. ?>
  7. <!doctype html>
  8. <html lang="en">
  9. <head>
  10. <meta charset="UTF-8">
  11. <meta name="viewport"
  12. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  13. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  14. <title>登录页面</title>
  15. </head>
  16. <body>
  17. <div class="login">
  18. <h3>提交</h3>
  19. <form action="handle.php?action=login" method="post">
  20. <div>
  21. <label for="email"></label>
  22. <input type="email" id="email" name="email" required >
  23. </div>
  24. <div>
  25. <label for="password"></label>
  26. <input type="password" id="password" name="password" required >
  27. </div>
  28. <div>
  29. <button>提交</button>
  30. </div>
  31. </form>
  32. </div>
  33. </body>
  34. </html>

注册代码

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>登录页面</title>
  9. </head>
  10. <body>
  11. <div class="login">
  12. <h3>提交</h3>
  13. <form action="handle.php?action=register" method="post" onsubmit="return compare()">
  14. <div>
  15. <label for="email"></label>
  16. <input type="email" id="email" name="email" required >
  17. </div>
  18. <div>
  19. <label for="password"></label>
  20. <input type="password" id="password1" name="password1" required >
  21. </div>
  22. <div>
  23. <label for="password"></label>
  24. <input type="password" id="password2" name="password2" required >
  25. </div>
  26. <div>
  27. <button>提交 </button><span id="tips"></span>
  28. </div>
  29. </form>
  30. </div>
  31. </body>
  32. <script>
  33. function compare(){
  34. // var p1=document.getElementById('password1').value.trim();
  35. // var p2=document.getElementById('password2').value.trim();
  36. // // console.log(a);
  37. if (document.forms[0].password1.value.trim() !== document.forms[0].password2.value.trim()) {
  38. document.querySelector('#tips').innerText = '二次密码不相等';
  39. return false;
  40. }
  41. }
  42. </script>
  43. </html>

处理登录注册退出的代码

  1. <?php
  2. $pdo=new PDO('mysql:host=localhost;dbname=phpedu','root','root');
  3. $stmt=$pdo->prepare('SELECT * FROM `user`' );
  4. $stmt->execute();
  5. //fecthAll 是获取所有结果集PDO::FETCH_ASSOC是用关联的方式
  6. $users=$stmt->fetchAll(PDO::FETCH_ASSOC);
  7. //print_r($users);
  8. $action=$_GET['action'];
  9. //echo $_SERVER['REQUEST_METHOD'];
  10. switch (strtolower($action)){
  11. case 'login';
  12. // $_SERVER['REQUEST_METHOD']判断是以什么方式提交上来的
  13. if ($_SERVER['REQUEST_METHOD']==='POST'){
  14. $name=$_POST['email'];
  15. $password=$_POST['password'];
  16. $results =array_filter($users ,function ($user ) use ( $name ,$password){
  17. return $user['name']===$name&&$user['password']===$password;
  18. });
  19. if (count($results)===1){
  20. setcookie('user',serialize(array_pop($results)));
  21. exit('<script>alert("验证通过");location.href="index.php"</script>');
  22. }else{
  23. exit('<script>alert("请重新登陆");location.href="login.php"</script>');
  24. }
  25. }else{
  26. die('请求类型错误');
  27. }
  28. break;
  29. // 退出
  30. case 'logout';
  31. if (isset($_COOKIE['user'])){
  32. setcookie("user", null, time() - 3600);
  33. exit('<script>alert("已经退出");location.href="index.php"</script>');
  34. }
  35. break;
  36. // 注册
  37. case 'register';
  38. $email=$_POST['email'];
  39. $name=$_POST['name'];
  40. $password=$_POST['password1'];
  41. $register_time=time();
  42. $sql="INSERT `user` SET `name`='{$name}',`email`='{$email}',`password`='{$password}',`register`='{$register_time}'";
  43. $stmt=$pdo->prepare($sql);
  44. if ($stmt->rowCount() === 1) exit('<script>alert("注册成功");location.assign("login.php")</script>');
  45. else exit('<script>alert("注册失败");location.assign("login.php")</script>');
  46. break;
  47. default:
  48. exit('未定义操作');
  49. }

session

核心代码

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

首页

  1. <?php
  2. // 开启会话
  3. session_start();
  4. // 判断是否已经登录?
  5. if (isset($_SESSION['user'])) $user = unserialize($_SESSION['user']);
  6. ?>
  7. <!doctype html>
  8. <html lang="en">
  9. <head>
  10. <meta charset="UTF-8">
  11. <meta name="viewport"
  12. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  13. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  14. <title>爬宠乐园</title>
  15. <style>
  16. *{text-decoration: none;margin: 0;padding: 0;}
  17. body{display: flex;flex-flow: column;font-weight: 800;}
  18. header{ box-sizing: border-box;padding: 10px 30px;width: 100%;display: flex;flex-flow: row nowrap;justify-content: space-between; background: lightseagreen; }
  19. /*.logo*/
  20. header div:nth-of-type(2){color: #ffd012;text-align: center;line-height: 42px }
  21. header div:nth-of-type(3){display: flex;justify-content: space-between;line-height: 42px }
  22. header div:nth-of-type(3) a{color:#ffd012; }
  23. </style>
  24. </head>
  25. <body>
  26. <header>
  27. <div class="logo"><h1>爬宠乐园</h1></div>
  28. <div>一个程序员的爱好社区</div>
  29. <div>
  30. <?php if (isset($user)):?>
  31. <a href=""><?php echo $user['name']?></a>&nbsp;&nbsp;
  32. <a href="" id="logout">退出</a>
  33. <?php else:?>
  34. <a href="login.php">登陆</a>&nbsp;&nbsp;
  35. <a href="register.php">注册</a>
  36. <?php endif;?>
  37. </div>
  38. </header>
  39. <script>
  40. document.querySelector('#logout').addEventListener('click',function (event) {
  41. if (confirm('确认退出吗?')) {
  42. // 禁用默认行为, 其实就是禁用原<a>标签的点击跳转行为,使用事件中的自定义方法处理
  43. event.preventDefault();
  44. // 跳转到退出事件处理器
  45. window.location.assign('handle.php?action=logout');
  46. }
  47. })
  48. </script>
  49. </body>
  50. </html>

登陆

  1. <?php
  2. // 开启会话
  3. session_start();
  4. // 判断是否已登录
  5. if (isset($_SESSION['user']))
  6. exit('<script>alert("请不要重复登录");location.href="index.php";</script>');
  7. ?>
  8. <!doctype html>
  9. <html lang="en">
  10. <head>
  11. <meta charset="UTF-8">
  12. <meta name="viewport"
  13. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  14. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  15. <title>登录页面</title>
  16. </head>
  17. <body>
  18. <div class="login">
  19. <h3>提交</h3>
  20. <form action="handle.php?action=login" method="post">
  21. <div>
  22. <label for="email"></label>
  23. <input type="email" id="email" name="email" required >
  24. </div>
  25. <div>
  26. <label for="password"></label>
  27. <input type="password" id="password" name="password" required >
  28. </div>
  29. <div>
  30. <button>提交</button>
  31. </div>
  32. </form>
  33. </div>
  34. </body>
  35. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议