Ajax validates the user name and password of the instance code _ajax related

Source: Internet
Author: User
Tags sha1 sha1 encryption

The example of this article introduced the AJAX authentication username and password of the specific code for your reference, the specific contents are as follows

1.ajax Body 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");
  The catch (e) {}}} function login () {createxmlhttprequest ();
  var user = document.getElementById ("YHM"). Value;
  var password = document.getElementById ("MM"). Value; if (user== "" | |
   password== "") {alert ("Please enter username and password!");
  return false;
  The 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 (' username or password is wrong! ');
        User= "";
        Password= "";
      return false;
      } else{window.location.href= "index1.html";
 }}} xmlrequest.send ("user=" +user+ "&password=" +password); }

2.html Code

<input placeholder= "username" autofocus= "type=" text "name=" username "> <input placeholder=  
  " password "type=" Password "name=" password ">
  <button id=" DL "onclick=" Login () "> Login </button>

3. Here is the use of SHA1 encryption, your password and database name changes to your own can

<?php
$yhm 1=$_post[' user '];
 $mm 1=$_post[' password '];
@ $DP =new mysqli (' localhost ', ' root ', ' Your password ', ' your database name ');
$yhm 2=sha1 ($yhm 1);
$MM 2=SHA1 ($mm 1);
$query = "SELECT * from Zhuce where yhm= ' $yhm 2 ' and mm= ' $mm 2 '";
$result = $DP->query ($query);
$num = $result->num_rows;
if (! $num) {
  echo "1";
}
 
$DP->close ();
 
? >

The above is the entire content of this article, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.