This time I will bring you the ajax implementation to verify the user name and password in the database. What are the precautions for the ajax implementation to verify the user name and password in the database. The following is a practical case, let's take a look.
The example of this article introduces the specific code of ajax verification user name and password for your reference. The specific content is as follows
1.ajax main part
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 code
<input placeholder="用户名" autofocus="" type="text"name="username"> <input placeholder="密码" type="password" name="password"> <button id="dl" onclick="login()">登录</button>
3. Sha1 encryption is used here, just change your password and database name to your own
<?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(); ?>
I believe you have mastered the method after reading the case in this article, more Please pay attention to other related articles on the php Chinese website!
Recommended reading:
Jump to the login page immediately after Ajax+Session fails
How to deal with Session failure when accessed by ajax
The above is the detailed content of Ajax implements verification of username and password in database. For more information, please follow other related articles on the PHP Chinese website!