Blogger Information
Blog 52
fans 1
comment 1
visits 38677
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
会话控制作业
小丑0o鱼
Original
259 people have browsed it
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
  7. </head>
  8. <body>
  9. <form id="loginForm">
  10. <table>
  11. <tr>
  12. <td><label for="username">用户名:</label></td>
  13. <td><input type="text" required autofocus placeholder="请输入用户名" name="username" id="username"></td>
  14. </tr>
  15. <tr>
  16. <td><label for="password">密码:</label></td>
  17. <td><input type="password" required autofocus placeholder="请输入密码" name="password" id="password"></td>
  18. </tr>
  19. <tr>
  20. <td><img src="gd_captcha.php" alt="验证码" onclick="reloadImg()" id="img"></td>
  21. <td><input type="text" name="captcha" id="captcha" placeholder="请输入验证码"></td>
  22. </tr>
  23. <tr>
  24. <td></td>
  25. <td><input type="button" value="登陆" id="btn" onclick="dologin()"></td>
  26. </tr>
  27. </table>
  28. </form>
  29. <script>
  30. // 重新生成验证码
  31. function reloadImg() {
  32. $('#img').attr('src', "gd_captcha.php?id="+Math.random());
  33. }
  34. // 回车登陆
  35. $('#btn').keydown(function (e) {
  36. if (e.keyCode == 13) {
  37. dologin();
  38. }
  39. });
  40. // 登陆
  41. function dologin() {
  42. let username = $.trim($('#username').val()),
  43. password = $.trim($('#password').val()),
  44. captcha = $.trim($('#captcha').val());
  45. if (username == '') {
  46. alert('请输入用户名');
  47. return false;
  48. }
  49. if (password == '') {
  50. alert('请输入密码');
  51. return false;
  52. }
  53. if (captcha == '') {
  54. alert('请输入验证码');
  55. return false;
  56. }
  57. $.post('dologin.php', {'username': username, 'password': password, 'captcha': captcha}, function (res) {
  58. if (res.code > 0) {
  59. alert(res.msg);
  60. reloadImg();
  61. } else {
  62. alert(res.msg);
  63. setTimeout(() => {
  64. window.location.href = 'index.php'
  65. }, 1000);
  66. }
  67. }, 'json')
  68. }
  69. </script>
  70. </body>
  71. </html>
  72. 2.处理登陆 dologin.php
  73. <?php
  74. require 'connect.php';
  75. session_start();
  76. $username = $_POST['username'];
  77. $password = $_POST['password'];
  78. $captcha = strtolower($_POST['captcha']);
  79. if ($username == '') {
  80. exit(json_encode(array('code' => 1, 'msg' => '用户名不能为空')));
  81. }
  82. if ($password == '') {
  83. exit(json_encode(array('code' => 1, 'msg' => '密码不能为空')));
  84. }
  85. if ($captcha == '') {
  86. exit(json_encode(array('code' => 1, 'msg' => '验证码不能为空')));
  87. }
  88. // 验证验证码
  89. if ($captcha != strtolower($_SESSION['captcha'])) {
  90. exit(json_encode(array('code' => 1, 'msg' => '验证码不正确')));
  91. }
  92. // 验证用户
  93. $sql = "SELECT `username`,`password` FROM `users` WHERE `username`= ?; ";
  94. $stmt = $pdo->prepare($sql);
  95. $stmt->execute([$username]);
  96. $user = $stmt->fetch();
  97. if ($user == false) {
  98. exit(json_encode(array('code' => 1, 'msg' => '用户名不存在')));
  99. }
  100. if (md5($user['username'].$password) != $user['password']) {
  101. exit(json_encode(array('code' => 1, 'msg' => '密码错误')));
  102. }
  103. // 验证成功后设置session
  104. $_SESSION['username'] = $username;
  105. exit(json_encode(array('code' => 0, 'msg' => '登陆成功')));
  106. connect.php
  107. $dsn = 'mysql:host=127.0.0.1;port=3306;dbname=user';
  108. $username = 'root';
  109. $password = 'root123';
  110. $pdo = new PDO($dsn, $username, $password);
  111. 3.登陆成功后到主页index.php
  112. <?php
  113. session_start();
  114. echo $_SESSION['username']. '登陆成功';
  115. ?>
  116. <button name="logout" id="logout">安全退出</button>
  117. <script>
  118. const btn = document.querySelector('#logout');
  119. btn.onclick = (() => location.href='dologout.php')
  120. </script>
  121. 4.安全退出dologout.php
  122. session_start();
  123. session_destroy();
  124. exit('退出成功');
Correction status:Uncorrected

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