Home > Web Front-end > JS Tutorial > body text

Example code for ajax verification of username and password

亚连
Release: 2018-05-24 09:42:41
Original
1847 people have browsed it

This article mainly introduces the example code of ajax verification user name and password in detail. Interested friends can refer to it

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

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Ajax file upload successfully solves browser compatibility issues

laypage front-end paging plug-in implements ajax asynchronous paging

Ajax method of sending and receiving binary byte stream data

The above is the detailed content of Example code for ajax verification of username and password. 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