博客列表 >session流程实践

session流程实践

P粉317509817
P粉317509817原创
2022年05月05日 10:51:00564浏览

代码

indx.php

  1. <?php
  2. session_start();
  3. if(isset($_SESSION['user'])) {
  4. $user = unserialize($_SESSION['user']);
  5. }
  6. ?>
  7. <!DOCTYPE html>
  8. <html lang="en">
  9. <head>
  10. <meta charset="UTF-8">
  11. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  12. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  13. <title>登录界面</title>
  14. <style>
  15. * {
  16. margin: 0;
  17. padding: 0;
  18. box-sizing: border-box;
  19. }
  20. a{
  21. text-decoration: none;
  22. width: 100%;
  23. height: 100%;
  24. }
  25. header {
  26. background-color: #2C3E50;
  27. padding: 0.5em 1em;
  28. display: flex;
  29. height: 12vh;
  30. }
  31. header .title {
  32. font-weight:300;
  33. font-style:normal;
  34. font-size: 35pt;
  35. color: aliceblue;
  36. letter-spacing: 1px;
  37. text-shadow: 1px 1px 1px #D0D0D0;
  38. margin-top: 15px;
  39. }
  40. @import url("https://fonts.googleapis.com/css?family=Source+Sans+Pro");
  41. figure {
  42. width: 200px;
  43. height: 60px;
  44. cursor: pointer;
  45. perspective: 500px;
  46. -webkit-perspective: 500px;
  47. margin-left: auto;
  48. margin-top: 10px;
  49. }
  50. figure div {
  51. height: 100%;
  52. transform-style: preserve-3d;
  53. -webkit-transform-style: preserve-3d;
  54. transition: 0.25s;
  55. -webkit-transition: 0.25s;
  56. }
  57. figure:hover div {
  58. transform: rotateX(-90deg);
  59. }
  60. span {
  61. width: 100%;
  62. height: 100%;
  63. position: absolute;
  64. box-sizing: border-box;
  65. border: 5px solid #fff;
  66. font-family: "Source Sans Pro", sans-serif;
  67. line-height: 50px;
  68. font-size: 17pt;
  69. text-align: center;
  70. text-transform: uppercase;
  71. }
  72. span:nth-child(1) {
  73. color: #fff;
  74. transform: translate3d(0, 0, 30px);
  75. -webkit-transform: translate3d(0, 0, 30px);
  76. }
  77. span:nth-child(2) {
  78. color: #1c175d;
  79. background: #fff;
  80. transform: rotateX(90deg) translate3d(0, 0, 30px);
  81. -webkit-transform: rotateX(90deg) translate3d(0, 0, 30px);
  82. }
  83. </style>
  84. </head>
  85. <body>
  86. <header>
  87. <?php if(isset($user)):?>
  88. <h2 class="title"><?= $user['email']?></h2>
  89. <?php else:?>
  90. <h2 class="title">请先登录</h2>
  91. <?php endif?>
  92. <figure>
  93. <div>
  94. <span>Hover Me</span>
  95. <?php if(isset($user)):?>
  96. <span><a href="" id="logout">退出</a></span>
  97. <?php else :?>
  98. <span><a href="login.php">登录</a></span>
  99. <?php endif?>
  100. </div>
  101. </figure>
  102. </header>
  103. <script>
  104. document.querySelector('#logout').addEventListener('click',function(event){
  105. if(confirm('是否退出?')){
  106. event.preventDefault();
  107. location.assign('handle.php?action=logout')
  108. }
  109. });
  110. </script>
  111. </body>
  112. </html>

login.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>登录界面</title>
  8. <style>
  9. form {
  10. display: inline-block;
  11. }
  12. a{
  13. text-decoration: none;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <form action="handle.php?action=login" method="POST">
  19. <fieldset>
  20. <legend>用户登录</legend>
  21. <p>
  22. <label for="">邮 箱:</label>
  23. <input type="emali" name="email">
  24. </p>
  25. <p>
  26. <label for="">密 码:</label>
  27. <input type="password" name="psw1" id="psw1">
  28. </p>
  29. <p>
  30. <button>登录</button>
  31. </p>
  32. <p>
  33. <a href="register.php">如果没有账号,请先注册>></a>
  34. </p>
  35. </fieldset>
  36. </form>
  37. </body>
  38. </html>

register.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>新用户注册</title>
  8. <style>
  9. form {
  10. display: inline-block;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <form action="handle.php?action=register" method="POST">
  16. <fieldset>
  17. <legend>用户注册</legend>
  18. <p>
  19. <label for="">邮&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;箱:</label>
  20. <input type="email" name="email" id="email">
  21. </p>
  22. <p>
  23. <label for="">密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码:</label>
  24. <input type="password" name="psw2" id="psw2" placeholder="密码必须大于8位">
  25. </p>
  26. <p>
  27. <label for="">确认密码:</label>
  28. <input type="password" name="" id="psw3" placeholder="再次输入密码">
  29. </p>
  30. <button>提交</button>
  31. </fieldset>
  32. </form>
  33. <script>
  34. document.querySelector('button').addEventListener('click',function(event){
  35. let psw1 = document.querySelector('#psw2');
  36. let psw2 = document.querySelector('#psw3');
  37. // let form = document.querySelector('form');
  38. if(psw1.value.length > 8) {
  39. if(psw1.value !== psw2.value){
  40. event.preventDefault();
  41. alert("两次密码输入不一致");
  42. location.assign('register.php');
  43. }
  44. }else{
  45. event.preventDefault();
  46. alert("密码长度不够");
  47. }
  48. })
  49. </script>
  50. </body>
  51. </html>

handle.php

  1. <?php
  2. // 开启会话
  3. session_start();
  4. // 链接数据库
  5. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  6. $stmt = $db->prepare('SELECT * FROM `login`');
  7. $stmt->execute();
  8. $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
  9. // print_r($users);
  10. // print_r($_GET);
  11. $action = $_GET['action'];
  12. switch(strtolower($action)){
  13. // 登录
  14. case 'login':
  15. // 验证数据是否是以POST请求发送
  16. if($_SERVER['REQUEST_METHOD']==='POST'){
  17. extract($_POST);
  18. $result = array_filter($users,function($user) use ($email,$psw1){
  19. return $user['email']===$email && $user['password']=== md5($psw1);
  20. });
  21. if(count($result)===1){
  22. $_SESSION['user']=serialize(array_pop($result));
  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. // 退出
  32. case 'logout':
  33. if(isset($_SESSION['user'])){
  34. session_destroy();
  35. exit('<script>alert("退出成功");location.assign("index.php")</script>');
  36. }
  37. break;
  38. // 注册
  39. case 'register':
  40. // 获取新用户的信息
  41. $email = $_POST['email'];
  42. $password =md5($_POST['psw2']);
  43. $register_time=time();
  44. // 查重
  45. $sql = 'SELECT `email` FROM `login`';
  46. $stmt=$db->prepare($sql);
  47. $stmt->execute();
  48. $staffs = $stmt->fetchAll(PDO::FETCH_ASSOC);
  49. $flag=true;
  50. printf('<pre>%s</pre>', print_r($staffs, true));
  51. foreach($staffs as $staff){
  52. if(in_array($email,$staff)){
  53. $flag = false;
  54. exit('<script>alert("邮箱已被注册");location.href="register.php"</script>');
  55. break;
  56. }
  57. }
  58. if($flag){
  59. // 添加到数据库中
  60. $sql = 'INSERT `login` SET `email`=?,`password`=?,`register_time`=? ';
  61. $stmt= $db->prepare($sql);
  62. $stmt->execute([$email,$password,$register_time]);
  63. if($stmt->rowCount()>0){
  64. exit('<script>alert("注册成功");location.assign("login.php")</script>');
  65. }else{
  66. exit('<script>alert("注册失败");location.assign("register.php")</script>');
  67. }
  68. }
  69. break;
  70. }

演示

1、先在数据库中插入一个已有数据

2、登录操作

3、注册操作


4、注册失败(密码长度不够)

5、注册失败(邮箱已被注册)

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