当前位置: 移动技术网 > IT编程>脚本编程>Ajax > ajax验证用户名和密码的实例代码

ajax验证用户名和密码的实例代码

2017年12月08日  | 移动技术网IT编程  | 我要评论
本文实例为大家介绍了ajax验证用户名和密码的具体代码,供大家参考,具体内容如下 1.ajax主体部分     v

本文实例为大家介绍了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代码    

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

3.这里用的是sha1加密,把你的密码和数据库名称修改成你自己的即可    

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

以上就是本文的全部内容,希望对大家的学习有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网