这次给大家带来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代码
登录
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中文网其它相关文章!
推荐阅读: