" class="reference-link">#流程分析图

入口文件index.php

  1. <?php
  2. session_start();
  3. // 判断用户是否已经登录?
  4. if (isset($_SESSION['user'])) $user = unserialize($_SESSION['user']);
  5. ?>
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8">
  10. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  12. <title>session会话处理</title>
  13. </head>
  14. <body>
  15. <nav>
  16. <?php if (isset($user)) : ?>
  17. <a href="" id="logout">退出</a>
  18. <?php else:?>
  19. <a href="login.php">登录</a>
  20. <?php endif ?>
  21. </nav>
  22. <script>
  23. document.querySelector('#logout').addEventListener('click', function(event) {
  24. if (confirm('是否退出?')) {
  25. // 阻止默认的点击事件执行
  26. event.preventDefault();
  27. // Location.assign() 方法会触发窗口加载并显示指定的URL的内容。
  28. location.assign('handle.php?action=logout');
  29. }
  30. });
  31. </script>
  32. </body>
  33. </html>

登录文件login.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <?php
  4. // 判断用户是否已经登录?
  5. if (isset($_SESSION['user'])) echo '<script>alert("不要重复登录");location.href="index.php"</script>';
  6. ?>
  7. <head>
  8. <meta charset="UTF-8">
  9. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  10. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  11. <title>Document</title>
  12. <style>
  13. #form1 {
  14. width: 20em;
  15. border-radius: 0.3em;
  16. box-shadow: 0 0 1em #888;
  17. box-sizing: border-box;
  18. padding: 1em 2em 1em;
  19. margin: 1em auto;
  20. background-color: lightblue;
  21. display: grid;
  22. grid-template-columns: 1fr;
  23. gap: 1em 0;
  24. }
  25. #form1 input {
  26. border-radius: 0.3em;
  27. border: none;
  28. padding-left: 0.3em;
  29. }
  30. #form1 input:focus {
  31. outline: none;
  32. box-shadow: 0 0 5px seagreen;
  33. background-color: hsl(283, 100%, 95%);
  34. border-radius: 0.2em;
  35. transition: 0.5s;
  36. }
  37. #form1 button {
  38. grid-area: auto / 2 / auto;
  39. outline: none;
  40. background-color: royalblue;
  41. border: none;
  42. height: 2em;
  43. color: #fff;
  44. }
  45. #form1 button:hover {
  46. cursor: pointer;
  47. background-color: seagreen;
  48. transition: 0.5s;
  49. }
  50. </style>
  51. </head>
  52. <body>
  53. <form id="form1" action="handle.php?action=login" method="POST" >
  54. <fieldset style="disply:inline-block;background:deepskyblue">
  55. <legend>用户登录</legend>
  56. <p>
  57. <input type="email" name="email" placeholder="user@email.com" required>
  58. </p>
  59. <p>
  60. <input type="password" name="password" placeholder="不少于六位" required>
  61. </p>
  62. <P>
  63. <button>提交</button>
  64. </P>
  65. </fieldset>
  66. <p>
  67. <a href="register.php">如果没有账号,请先注册!</a>
  68. </p>
  69. </form>
  70. </body>
  71. </html>

注册文件register.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <?php
  4. session_start();
  5. // 判断用户是否已经登录?
  6. if (isset($_SESSION['user'])) echo '<script>alert("不要重复登录");locatoin.href="index.php"</script>'
  7. ?>
  8. <head>
  9. <meta charset="UTF-8">
  10. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  12. <title>用户注册表单</title>
  13. <style>
  14. #form1 {
  15. width: 25em;
  16. border-radius: 0.3em;
  17. box-shadow: 0 0 1em #888;
  18. box-sizing: border-box;
  19. padding: 1em 2em 1em;
  20. margin: 1em auto;
  21. background-color: lightblue;
  22. display: grid;
  23. grid-template-columns: 1fr;
  24. gap: 1em 0;
  25. }
  26. #form1 fieldset {
  27. border: none;
  28. border-radius: 0.3em;
  29. box-shadow: 0 0 1em #888;
  30. box-sizing: border-box;
  31. display: grid;
  32. grid-template-columns: 5em 1fr;
  33. gap: 1em 0;
  34. padding: 1em 2em 1em;
  35. margin: 1em 1em;
  36. }
  37. #form1 fieldset legend
  38. {
  39. grid-area: auto / span 2;
  40. }
  41. #form1 input {
  42. border-radius: 0.3em;
  43. border: none;
  44. padding-left: 0.3em;
  45. }
  46. #form1 input:focus {
  47. outline: none;
  48. box-shadow: 0 0 5px seagreen;
  49. background-color: hsl(283, 100%, 95%);
  50. border-radius: 0.2em;
  51. transition: 0.5s;
  52. }
  53. #btn1 {
  54. grid-area: auto / 2 / auto;
  55. outline: none;
  56. background-color: royalblue;
  57. border: none;
  58. height: 2em;
  59. color: #fff;
  60. }
  61. #btn1:hover {
  62. cursor: pointer;
  63. background-color: seagreen;
  64. transition: 0.5s;
  65. }
  66. </style>
  67. </head>
  68. <body>
  69. <form id="form1" action="handle.php?action=register" method="POST">
  70. <fieldset style="disply:inline-block;background:deepskyblue">
  71. <legend>用户注册</legend>
  72. <label for="name">姓名:</label>
  73. <input type="text" name="name" id="username" placeholder="username" required>
  74. <label for="email">邮箱:</label>
  75. <input type="email" name="email" id="email" placeholder="user@email.com" required>
  76. <label for="password">密码:</label>
  77. <input type="password" name="password" id="psw1" placeholder="不少于六位" required>
  78. <label for="psw1">确认密码:</label>
  79. <input type="password" name="psw1" id="psw2" onblur="check()" placeholder="*两次一致√" required>
  80. <input type="submit" id="btn1" onclick="check()">
  81. </fieldset>
  82. </form>
  83. <script type="text/javascript">
  84. //自定义通过ID获取元素的函数
  85. function $(id){
  86. return document.getElementById(id)
  87. }
  88. function check(){
  89. let check;
  90. if ($('psw1').value != $('psw2').value) {
  91. alert('两次密码不一致,请重新输入');
  92. $('psw1').focus();
  93. check=false;
  94. }else{
  95. alert('两次密码一致');
  96. check=true;
  97. }
  98. return check;
  99. }
  100. </script>
  101. </body>
  102. </html>

注册验证效果图示

控制器文件handle.php

  1. <?php
  2. // 开启会话
  3. // 控制器文件:根据用户的不同请求,执行不同的操作,登录 , 注册, 退出
  4. // 连接数据并获取用户表中的数据
  5. session_start();
  6. $db = new PDO('mysql:dbname=phpedu','root','root');
  7. $stmt = $db->prepare('SELECT * FROM `user`');
  8. $stmt->execute();
  9. $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
  10. $action = $_GET['action'];
  11. switch (strtolower($action)) {
  12. // 登录
  13. case 'login':
  14. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  15. extract($_POST);
  16. $result = array_filter($users, function($user) use ($email,$password) {
  17. return $user['email'] === $email && $user['password'] === md5($password);
  18. });
  19. if (count($result) === 1) {
  20. $_SESSION['user'] = serialize(array_pop($result));
  21. // exit、die — 输出一个消息并且退出当前脚本
  22. exit('<script>alert("验证通过");location.href="index.php"</script>');
  23. } else {
  24. exit('<script>alert("邮箱或密码错误");location.href="index.php"</script>');
  25. }
  26. } else {
  27. die('请求错误');
  28. }
  29. break;
  30. // 退出
  31. case 'logout':
  32. if (isset($_SESSION['user'])) {
  33. session_destroy();
  34. exit('<script>alert("退出成功");location.assign("index.php")</script>');
  35. }
  36. break;
  37. // 注册
  38. case 'register':
  39. $name = $_POST['name'];
  40. $email = $_POST['email'];
  41. $password = md5($_POST['password']);
  42. $register_time = time();
  43. $sql = 'INSERT `user` SET `name` = ?, `email` = ?, `password` = ?,`time` = ?';
  44. $stmt = $db->prepare($sql);
  45. $stmt->execute([$name, $email, $password, $register_time]);
  46. if ($stmt->rowCount() === 1) {
  47. echo '<script>alert("注册成功");location.href="index.php"</script>';
  48. } else {
  49. exit('<script>alert("注册失败");location.href="index.php"</script>');
  50. }
  51. break;
  52. }

更多相关文章

  1. Android(安卓)逆向apk程序的心得
  2. Android(安卓)渗透测试学习手册(三)Android(安卓)应用的逆向和审计
  3. 最新res索引讲解(drawable、layout、values等目录的分辨率和layou
  4. Android之Adapter用法总结
  5. Windows下的Android(安卓)SDK下载,2.2之前各个版本及Google API,文
  6. 谈谈android数据存储方式
  7. Android(安卓)系统权限之SuperSU 模拟器root
  8. Android(安卓)Building System 总结
  9. Android虚拟机与Java虚拟机——两种虚拟机的比较

随机推荐

  1. 的良好实践是什么?它可以替换还是只用于
  2. HTML5的重点知识小结——整体布局(浮动布
  3. 浏览器加载和渲染html的顺序
  4. 用正则表达式剔除文本里面HTML标记
  5. UpdateResource(增加、删除或替代某可执
  6. HTML中的自定义HTTP请求标头
  7. 如何优化生成的文档形式html和样式
  8. html 防止表格或div被撑开
  9. 怎样把Excel表格转换成html网页格式输出?
  10. 如何设置div的样式以使其与文本一起运行?