首頁  >  文章  >  web前端  >  ajax驗證使用者名稱和密碼的實例代碼

ajax驗證使用者名稱和密碼的實例代碼

亚连
亚连原創
2018-05-24 09:42:411793瀏覽

這篇文章主要為大家詳細介紹了ajax驗證用戶名和密碼的實例代碼,有興趣的小伙伴們可以參考一下

本文實例為大家介紹了ajax驗證用戶名和密碼的具體代碼,供大家參考,內容如下

1.ajax主體部分    

var xmlrequest;
function createXMLHttpRequest(){
      if(window.XMLHttpRequest){
       xmlrequest=new XMLHttpRequest();
      }
      else if(window.ActiveXObject){
         try{
           xmlrequest=new ActiveXObject("Msxm12.XMLHTTP");
         }
         catch(e){
            try{
             xmlrequest=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){}
         }
      
      }
}
function login(){   
 createXMLHttpRequest();
  var user = document.getElementById("yhm").value;
  var password = document.getElementById("mm").value;
  if(user==""||password==""){
   alert("请输入用户名和密码!");
   return false;
  }
  var url = "check.php?user="+user+"&password="+password;
  xmlrequest.open("POST",url,true);
  xmlrequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
   xmlrequest.onreadystatechange = function(){
  if(xmlrequest.readyState == 4){
   if(xmlrequest.status==200){
      var msg = xmlrequest.responseText;   
        if(msg=='1'){
        alert('用户名或密码错误!');
        user="";
        password="";
        return false;
      } 
      else{       
        window.location.href="index1.html";
      }
    }
  }
 }
  xmlrequest.send("user="+user+"&password="+password);
 }

2.html代碼    

#2.html代碼    


<input placeholder="用户名" autofocus="" type="text"name="username">  
  <input placeholder="密码" type="password" name="password">
  <button id="dl" onclick="login()">登录</button>

3.這裡用的是sha1加密,把你的密碼和資料庫名稱修改成你自己的即可    

#
<?php
$yhm1=$_POST[&#39;user&#39;];
 $mm1=$_POST[&#39;password&#39;];
@ $dp=new mysqli(&#39;localhost&#39;,&#39;root&#39;,&#39;你的密码&#39;,&#39;你的数据库名称&#39;);
$yhm2=sha1($yhm1);
$mm2=sha1($mm1);
$query="select * from zhuce where yhm=&#39;$yhm2&#39; and mm=&#39;$mm2&#39;";
$result=$dp->query($query);
$num=$result->num_rows;
if(!$num){
  echo "1";
}
 
$dp->close();
 
?>

上面是我整理給大家的,希望今後對大家有幫助。 相關文章:

ajax檔案上傳成功解決瀏覽器相容問題

#laypage前端分頁外掛實現ajax非同步分頁

Ajax發送和接收二進位位元組流資料的方法

######## ###

以上是ajax驗證使用者名稱和密碼的實例代碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn