亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 32
粉絲 2
評(píng)論 0
訪問量 37119
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
用戶登錄驗(yàn)證簡(jiǎn)單案例
簡(jiǎn)行
原創(chuàng)
1387人瀏覽過

用戶登錄驗(yàn)證簡(jiǎn)單案例

文檔說明(圖片):

1.Connect.php

  1. <?php
  2. namespace demo_MVC;
  3. //數(shù)據(jù)庫(kù)參數(shù)
  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. //連接數(shù)據(jù)
  16. public function link(){
  17. try{
  18. //連接數(shù)據(jù)款
  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. //捕捉特定于數(shù)據(jù)庫(kù)信息的PDOEXCEPTION 異常
  22. echo $e->getMessage();
  23. } catch(\Throwable $e){
  24. //捕捉擁有Throwable接口的錯(cuò)誤或者其他異常
  25. echo $e->getMessage();
  26. }
  27. }
  28. }

2.Model.php

  1. <?php
  2. namespace demo_MVC;
  3. //加載數(shù)據(jù)庫(kù)連接
  4. require "./connect.php";
  5. //開啟session會(huì)話
  6. session_start();
  7. class Model extends Connect
  8. {
  9. //驗(yàn)證
  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'] ="賬號(hào)或密碼有誤";
  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'] = '驗(yàn)證碼有誤';
  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會(huì)話
  5. session_start();
  6. //獲取驗(yàn)證圖片
  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">賬號(hào):</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">驗(yàn)證碼:</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>登錄結(jié)果:</span>
  73. <span></span>
  74. </div>
  75. </body>
  76. <script type="text/javascript">
  77. //立即調(diào)用函數(shù)
  78. (function () {
  79. getcode();
  80. })();
  81. //ajax--GET請(qǐng)求方式獲取驗(yàn)證碼圖片
  82. function getcode() {
  83. //創(chuàng)建ajax對(duì)象
  84. var xhr = new XMLHttpRequest();
  85. //監(jiān)聽請(qǐng)求
  86. xhr.onreadystatechange = function () {
  87. if (xhr.readyState == 4 && xhr.status == 200) {
  88. // // xhr.responseText:保存請(qǐng)求成功后的文本數(shù)據(jù),是個(gè)json字符串,必須用JSON.parse()轉(zhuǎn)換js對(duì)象
  89. var data = JSON.parse(xhr.responseText);
  90. document.getElementById("codeimg").src = data["code"];
  91. }
  92. };
  93. //初始化請(qǐng)求參數(shù)
  94. xhr.open("GET", "code.php");
  95. //發(fā)送請(qǐng)求
  96. xhr.send(null);
  97. }
  98. //ajax--POST請(qǐng)求方式驗(yàn)證登錄
  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:保存請(qǐng)求成功后的文本數(shù)據(jù),是個(gè)json字符串,必須用JSON.parse()轉(zhuǎn)換js對(duì)象
  106. var obj = JSON.parse(xhr.responseText);
  107. var reslut = document.querySelector(".reslut");
  108. reslut.children[1].innerText = obj["msg"];
  109. }
  110. };
  111. // 初始化請(qǐng)求參數(shù)
  112. xhr.open("POST", "Model.php");
  113. // 使用FormData來組織數(shù)據(jù),最終仍是以表單數(shù)據(jù)方式發(fā)送
  114. var data = new FormData(form);
  115. //發(fā)送請(qǐng)求
  116. xhr.send(data);
  117. });
  118. </script>
  119. </html>

效果圖:

批改老師:天蓬老師天蓬老師

批改狀態(tài):合格

老師批語(yǔ):做之前,做好規(guī)劃, 會(huì)好很多
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)