博客列表 >用户登录验证简单案例

用户登录验证简单案例

简行
简行原创
2020年08月14日 22:45:55691浏览

用户登录验证简单案例

文档说明(图片):

1.Connect.php

  1. <?php
  2. namespace demo_MVC;
  3. //数据库参数
  4. class Connect{
  5. private $param =[
  6. 'DB_HOST'=>'localhost',
  7. 'DB_TYPE'=>'mysql',
  8. 'DB_NAME'=>'my_user',
  9. 'DB_PASSWORD'=>'root123',
  10. 'DB_USER'=>'root',
  11. 'DB_CHARSET'=>'utf8',
  12. 'DB_PORT'=>'3306'
  13. ];
  14. // public $dsn = "mysql:host=localhost;dbname=my_user;charset=utf8";
  15. //连接数据
  16. public function link(){
  17. try{
  18. //连接数据款
  19. return $pdo = new \PDO("mysql:host=localhost;dbname=my_user;charset=utf8",$this->param['DB_USER'],$this->param['DB_PASSWORD']);
  20. } catch(\PDOException $e){
  21. //捕捉特定于数据库信息的PDOEXCEPTION 异常
  22. echo $e->getMessage();
  23. } catch(\Throwable $e){
  24. //捕捉拥有Throwable接口的错误或者其他异常
  25. echo $e->getMessage();
  26. }
  27. }
  28. }

2.Model.php

  1. <?php
  2. namespace demo_MVC;
  3. //加载数据库连接
  4. require "./connect.php";
  5. //开启session会话
  6. session_start();
  7. class Model extends Connect
  8. {
  9. //验证
  10. public function checked($data){
  11. $pdo = parent::link();
  12. $sql = "SELECT * FROM `mu_user` WHERE `username`=?";
  13. $stm = $pdo ->prepare($sql);
  14. $stm ->bindParam(1,$data['username']);
  15. $stm ->execute();
  16. $result = $stm ->fetch();
  17. if($result && $result['password'] == md5($data['password'])){
  18. $arr['status'] =200;
  19. $arr['msg'] ="登录成功";
  20. }else{
  21. $arr['status'] =100;
  22. $arr['msg'] ="账号或密码有误";
  23. }
  24. return $arr;
  25. }
  26. }
  27. if(strtolower($_POST['code']) == strtolower($_SESSION['codenum'])){
  28. $model = new Model();
  29. $res = $model->checked($_POST);
  30. }else{
  31. $res['status'] = 100;
  32. $res['msg'] = '验证码有误';
  33. }
  34. echo json_encode($res);

3.code.php

  1. <?php
  2. require_once __DIR__.'/composer/vendor/autoload.php';
  3. use Gregwar\Captcha\CaptchaBuilder;
  4. //开启session会话
  5. session_start();
  6. //获取验证图片
  7. $captcha = new CaptchaBuilder;
  8. $captcha->build()->save('out.jpg');
  9. $_SESSION["codenum"] = $captcha ->getPhrase();
  10. $data['code'] = $captcha ->inline();
  11. echo json_encode($data);

4.login.html

  1. <?php
  2. require "./code.php";
  3. ?>
  4. <!DOCTYPE html>
  5. <html lang="en">
  6. <head>
  7. <meta charset="UTF-8" />
  8. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  9. <title>登录</title>
  10. </head>
  11. <style>
  12. h2 {
  13. text-align: center;
  14. }
  15. form {
  16. width: 350px;
  17. border: 1px solid;
  18. margin-left: 38%;
  19. background-color: beige;
  20. }
  21. form div {
  22. margin: 15px 15px;
  23. display: flex;
  24. flex-direction: column;
  25. }
  26. form div input {
  27. margin: 10px 0px;
  28. }
  29. img {
  30. width: 200px;
  31. }
  32. img:hover {
  33. cursor: pointer;
  34. }
  35. button:hover {
  36. cursor: pointer;
  37. }
  38. span {
  39. font-size: 1.5rem;
  40. }
  41. .reslut {
  42. width: 350px;
  43. margin-left: 38%;
  44. margin-top: 20px;
  45. background-color: rgb(147, 197, 193);
  46. }
  47. .reslut > span:nth-of-type(2) {
  48. color: chocolate;
  49. }
  50. </style>
  51. <body>
  52. <h2>登 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;录</h2>
  53. <form action="" method="POST" onsubmit="return false">
  54. <div>
  55. <label for="username">账号:</label>
  56. <input type="text" name="username" id="username" />
  57. </div>
  58. <div>
  59. <label for="password">密码:</label>
  60. <input type="password" name="password" id="password" />
  61. </div>
  62. <div>
  63. <label for="code">验证码:</label>
  64. <input type="text" name="code" id="code" />
  65. <img src="" onclick="getcode()" id="codeimg" />
  66. </div>
  67. <div>
  68. <button>登录</button>
  69. </div>
  70. </form>
  71. <div class="reslut">
  72. <span>登录结果:</span>
  73. <span></span>
  74. </div>
  75. </body>
  76. <script type="text/javascript">
  77. //立即调用函数
  78. (function () {
  79. getcode();
  80. })();
  81. //ajax--GET请求方式获取验证码图片
  82. function getcode() {
  83. //创建ajax对象
  84. var xhr = new XMLHttpRequest();
  85. //监听请求
  86. xhr.onreadystatechange = function () {
  87. if (xhr.readyState == 4 && xhr.status == 200) {
  88. // // xhr.responseText:保存请求成功后的文本数据,是个json字符串,必须用JSON.parse()转换js对象
  89. var data = JSON.parse(xhr.responseText);
  90. document.getElementById("codeimg").src = data["code"];
  91. }
  92. };
  93. //初始化请求参数
  94. xhr.open("GET", "code.php");
  95. //发送请求
  96. xhr.send(null);
  97. }
  98. //ajax--POST请求方式验证登录
  99. var btn = document.querySelector("form button");
  100. var form = document.querySelector("form");
  101. btn.addEventListener("click", function () {
  102. var xhr = new XMLHttpRequest();
  103. xhr.onreadystatechange = function () {
  104. if (xhr.readyState == 4 && xhr.status == 200) {
  105. // // xhr.responseText:保存请求成功后的文本数据,是个json字符串,必须用JSON.parse()转换js对象
  106. var obj = JSON.parse(xhr.responseText);
  107. var reslut = document.querySelector(".reslut");
  108. reslut.children[1].innerText = obj["msg"];
  109. }
  110. };
  111. // 初始化请求参数
  112. xhr.open("POST", "Model.php");
  113. // 使用FormData来组织数据,最终仍是以表单数据方式发送
  114. var data = new FormData(form);
  115. //发送请求
  116. xhr.send(data);
  117. });
  118. </script>
  119. </html>

效果图:

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