温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ajax如何验证用户名和密码

发布时间:2021-09-01 11:25:25 来源:亿速云 阅读:111 作者:小新 栏目:web开发

这篇文章主要介绍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();
 
?>

以上是“ajax如何验证用户名和密码”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI