首页 > web前端 > js教程 > 正文

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

亚连
发布: 2018-05-24 09:42:41
原创
1797 人浏览过

这篇文章主要为大家详细介绍了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[&#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中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!