这次给大家带来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();
?>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
ajax+session失效后即刻跳转登录页面
ajax访问到session失效如何处理
以上就是ajax实现验证数据库里的用户名和密码的详细内容。