PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

ajax实现验证数据库里的用户名和密码

php中世界最好的语言
php中世界最好的语言 原创
2018-04-04 14:59:53 4784浏览

这次给大家带来ajax实现验证数据库里的用户名和密码,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代码

<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();
 
?>

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

Ajax+Session失效后即刻跳转登录页面

ajax访问到Session失效如何处理

以上就是ajax实现验证数据库里的用户名和密码的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。