Home > Web Front-end > JS Tutorial > Ajax implements verification of username and password in database

Ajax implements verification of username and password in database

php中世界最好的语言
Release: 2018-04-04 14:59:53
Original
5198 people have browsed it

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);
 }
Copy after login

2.html code

<input placeholder="用户名" autofocus="" type="text"name="username">  
  <input placeholder="密码" type="password" name="password">
  <button id="dl" onclick="login()">登录</button>
Copy after login

3. Sha1 encryption is used here, just change your password and database name to your own

<?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();
 
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template