PHP/AJAX——登录页面与登录信息提示(非安全版本)
生活随笔
收集整理的這篇文章主要介紹了
PHP/AJAX——登录页面与登录信息提示(非安全版本)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
基本概念
AJAX:AJAX引擎其實是一個JavaScript對象,全寫是 window.XMLHttpRequest對象,由于瀏覽器的版本不同,特別是老版本的IE瀏覽器,雖然也支持AJAX引擎,但是寫法上有區(qū)別,在IE低版本中通常用 ActiveXObject對象來創(chuàng)建AJAX引擎。 AJAX?來自英文“Asynchronous Javascript And XML”?的縮寫,也稱為異步JavaScript和XML。?簡言之,就是一個JS對象,可以實現(xiàn)在網(wǎng)頁加載完成以后,不用刷新的情況下與服務器交互。產(chǎn)生極好的用戶體驗效果。
示例
前端
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>維修預約后臺登錄_ZSTUCA</title><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"><link href="\assets\css\bootstrap.min.css" rel="stylesheet"><link href=".\assets\css\style.css" rel="stylesheet"></head> <body><div class="container" style="text-align:center"><!-- 登錄框 --><h1>計算機協(xié)會維修部</h1><h2>維修預約后臺登錄</h2><form class="form-signin" action="check.php" method="post"><p><label for="username" class="sr-only" title="用戶名">用戶名</label><input type="text" id="username" name="username" class="form-control" placeholder="用戶名" required autofocus> </p><p><label for="Password" class="sr-only" title="密碼">密碼</label><input type="password" id="password" name="password" class="form-control" placeholder="密碼" required></p><div style="min-height:25px;"><span id="result"></span></div><p><button class="btn btn-lg btn-primary" type="submit" style="margin:0 50px 0 0 ;">登錄</button><button class="btn btn-lg btn-primary" type="reset" style="margin:0 0 0 50px ;">重置</button></p></form><div class="footer">計算機協(xié)會維修隊</div></div> <script type="text/javascript">var username=document.getElementById('username');//用戶名輸入框失去焦點時觸發(fā) console.log(username); username.addEventListener('blur',function(){console.log("οnblur");//主角登場——AJAX引擎的創(chuàng)建及使用詳細代碼來了var ajax=new XMLHttpRequest(); //創(chuàng)建AJAX引擎實例//創(chuàng)建GET請求,發(fā)送請求時傳username值ajax.open('GET','check.php?username='+this.value); //當AJAX引擎的狀態(tài)產(chǎn)生改變時觸發(fā)onreadystatechange屬性指向的函數(shù)(多次執(zhí)行)//狀態(tài)值有5個:0 1 2 3 4 ,其中4表示服務器端響應就緒ajax.onreadystatechange=function(){//必須在服務器響應就緒,并且HTTP的狀態(tài)碼是200時才接收數(shù)據(jù)//ajax.readyState 獲取到服務器響應狀態(tài)碼,必須是4才表示就緒//ajax.status 獲取到HTTP的狀態(tài)碼,必須是200才表示成功if(ajax.readyState==4 && ajax.status==200){//ajax.responseText 接收服務器響應回來的內(nèi)容console.log(ajax.responseText);console.log(ajax.responseText=='1');//接收到服務器響應數(shù)據(jù)后,AJAX工作已完成,可根據(jù)結(jié)果顯示提示信息if(ajax.responseText==1){result.innerHTML='恭喜你,可以登錄';result.style.color='#0a0'; //將字體設置為紅色}else if(ajax.responseText==2){result.innerHTML='請輸入用戶名';result.style.color='#f00'; //將字體設置為紅色}else{result.innerHTML='該用戶名不存在,請重新輸入';result.style.color='#f00'; //將字體設置為綠色}}}ajax.send(); //發(fā)送請求});</script></body></html>css
@charset "gb2312"; /* CSS Document */body {padding-top: 40px;padding-bottom: 40px;background-color: #eee;}.form-signin {max-width: 400px;padding: 15px;margin: 0 auto;margin-top: 100px;border-style: solid; border-width: 3px;border-color: aliceblue;border-radius: 5%;background-color: azure;}.form-signin p{padding: 15px;margin: 0 auto;}.form-signin .form-signin-heading{margin-bottom: 10px;max-width: auto;}.form-signin .checkbox {margin-bottom: 10px;}.form-signin .checkbox {font-weight: normal;}.form-signin .form-control {position: relative;height: auto;-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;padding: 10px;font-size: 16px;}.form-signin .form-control:focus {z-index: 2;}.form-reg {max-width: 300px;padding: 15px;margin: 0 auto;}.form-reg .form-signin-heading{max-width: 300px;width: 300px;}.form-reg .checkbox {margin-bottom: 10px;}.form-reg .checkbox {font-weight: normal;}.form-reg .form-control {position: relative;height: auto;-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;padding: 10px;font-size: 16px;}.form-reg .form-control:focus {z-index: 2;}.form-reg input[type="email"] {margin-bottom: -1px;border-bottom-right-radius: 0;border-bottom-left-radius: 0;}.form-reg input[type="password"] {margin-bottom: 10px;border-top-left-radius: 0;border-top-right-radius: 0;}.login_lbl {visibility: hidden;}.footer{white-space: nowrap;vertical-align: middle;position: absolute;color: white;background-color: rgba(0,0,0,0.3);left: 0;right: 0;bottom: 33px;margin: auto;width: 175px;height: 22px;line-height: 20px;text-align: center;}?
后端
<?phpfunction checkdata($username){//連接數(shù)據(jù)庫的代碼include 'conn.php';$sql="select ID from wxyy where ID=".$username;$rs=mysqli_query($connID,$sql);//將構(gòu)造好的SQL語句發(fā)到服務器上執(zhí)行if( mysqli_num_rows($rs) ){echo '1'; //如果用戶名找到有結(jié)果,證明該用戶名已存在,返回1}else{echo '0'; //如果用戶名未找到結(jié)果,證明該用戶名不存在,返回0}//關閉數(shù)據(jù)庫連接,釋放結(jié)果集mysqli_free_result($rs);mysqli_close($connID); } //服務器端的代碼可以使用PHP編寫,根據(jù)邏輯反饋數(shù)據(jù)給客戶端實現(xiàn)驗證功能 if(isset($_GET['username']) and $_GET['username']!=''){checkdata($_GET['username']); }else{echo '2'; } ?>?
效果
說明
因為每次輸入用戶名即可返回用戶名數(shù)據(jù)庫中是否存在,容易被套取用戶數(shù)據(jù)。
參考文章
https://www.itsource.cn/web/news/5/20170512/1246.html
https://blog.csdn.net/weixin_43272781/article/details/102398525
總結(jié)
以上是生活随笔為你收集整理的PHP/AJAX——登录页面与登录信息提示(非安全版本)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP——访问网站根目录解决方案
- 下一篇: 动态规划算法php,php算法学习之动态