Web registration page Dynamic matching verification of user authentication PHP implementation (full version) two

Source: Internet
Author: User
This article mainly introduces the Web registration page dynamic matching verification of the user authentication PHP implementation (full version) Two, has a certain reference value, now share to everyone, the need for friends can refer to

Introduction:

If you are in the Web registration page Dynamic matching verification of the user verification PHP implementation of a place that is not understood, welcome to add me qq:363491343 understand.

See this article with the Web registration page dynamic matching validation of the user authentication PHP implementation is not too big, you can also directly read this article.

Body:

1. We create a first form, do the various input boxes:

Code: (Style.. Omission of irrelevant content)

<form action= "register.php" method= "post" class= "Subscribe-form" > <p class= "Row form-section" > <p class             = "Col-md-7 col-sm-7 col-xs-7" > <input name= "username" type= "text" class= "Form-control" id= "Contact_username" Onkeyup= "Loadxmldoc (this.value,this.id)" onblur= "uppercase ()" placeholder= "User name" required/> <span id= "             Txthint_name "></span> <input name=" password "type=" password "class=" Form-control "id=" Contact_password " Onkeyup= "Loadxmldoc (this.value,this.id)" onblur= "uppercase ()" placeholder= "Please enter password" required/> <span Id= "Txthint_pass" ></span> <input name= "password_" type= "password" class= "Form-control" id= "contact_      Password_ "onkeyup=" Loadxmldoc (this.value,this.id) "onblur=" uppercase () "placeholder=" Enter confirmation password "required/> <span id= "Txthint_pass_" ></span> <input name= "Phone" type= "text" class= "Form-control" id= "contact_p Hone "onkeyup=" loadXmlDoc (this.value,this.id) "onblur=" uppercase () "placeholder=" Please enter mobile number "required/> <span id=" TxtHint_phone " ></span> <input name= "Email" type= "text" class= "Form-control" id= "Contact_email" onkeyup= "loa Dxmldoc (this.value,this.id) "onblur=" uppercase () "placeholder=" Fill in the Mailbox "required/> <span id=" TxtHint_email ">& lt;/span> </p> </p> <p class= "col-md-5 col-sm-5 col-xs-5" > <button type= "Submit" class= "tm- Btn-subscribe "name=" register "> Registration </button> </p></form>

Code Explanation:

<span id= "Txthint_name" ></span>

<span id = ' txthint_name ' ></span> used to display the validation prompt, the two parameters in Loadxmldoc (this.value,this.id) are

Enter the contents of the box, and the ID name of the input box. The Uppercass () method is used to:

Hide cue message when focus leaves the input box
   When the focus leaves the input box, hide the hint message function uppercase () {       //event.target.id get the ID name       if (event.target.id== "Contact_username") {           //responsetext Gets the response data in the form of a string.           document.getElementById ("Txthint_name"). innerhtml= "";       } else if (event.target.id== "Contact_password") {        document.getElementById ("Txthint_pass"). innerhtml= "";    }      else if (event.target.id== "Contact_password_") {        document.getElementById ("Txthint_pass_"). innerhtml= "";    }       else if (event.target.id== "Contact_phone") {        document.getElementById ("Txthint_phone"). innerhtml= "";    }       else if (event.target.id== "Contact_email") {        document.getElementById ("Txthint_email"). innerhtml= "";    }}

As follows:

(Authentication prompt)


(Validation successfully hidden)


2.js implementations

(Here is a separate JS file, easy to manage)

function Loadxmldoc (str,id) {if (str.length==0) {document.getElementById ("Txthint_name"). innerhtml= "";                document.getElementById ("Txthint_pass"). innerhtml= "";                document.getElementById ("Txthint_pass_"). innerhtml= "";                document.getElementById ("Txthint_phone"). innerhtml= "";                document.getElementById ("Txthint_email"). innerhtml= "";            Return            } var xmlhttp; Check if the browser supports the XMLHttpRequest object if (window.        XMLHttpRequest) {//ie7+, Firefox, Chrome, Opera, Safari execute code xmlhttp=new xmlhttprequest ();        } else {//IE6, IE5 Browser execution code xmlhttp=new activexobject ("Microsoft.XMLHTTP");            } xmlhttp.onreadystatechange=function () {if (xmlhttp.readystate==4 && xmlhttp.status==200) {                        if (id== "Contact_username") {//responsetext Gets the response data in the form of a string. DocuMent.getelementbyid ("Txthint_name"). Innerhtml=xmlhttp.responsetext;                             } else if (id== "Contact_password") {document.getElementById ("Txthint_pass"). Innerhtml=xmlhttp.responsetext; } else if (id== "Contact_password_") {document.getElementById ("Txthint_pass_"). I                            Nnerhtml=xmlhttp.responsetext;                            } else if (id== "Contact_phone") {document.getElementById ("Txthint_phone"). Innerhtml=xmlhttp.responsetext; } else if (id== "Contact_email") {document.getElementById ("Txthint_email"). Innerht                            Ml=xmlhttp.responsetext; }}} xmlhttp.open ("GET", "..")        /common/verify.php?v= "+str+" &id= "+id,true);    Xmlhttp.send ();} When the focus leaves the input box, hide the hint message function uppercase () {//event.target.id get the ID name if (event.target.id== "Contact_usernam             E ") {//responsetext Gets the response data as a string. Document.geteleMentbyid ("Txthint_name"). innerhtml= "";              } else if (event.target.id== "Contact_password") {document.getElementById ("Txthint_pass"). innerhtml= "";              } else if (event.target.id== "Contact_password_") {document.getElementById ("Txthint_pass_"). innerhtml= "";              } else if (event.target.id== "Contact_phone") {document.getElementById ("Txthint_phone"). innerhtml= "";              } else if (event.target.id== "Contact_email") {document.getElementById ("Txthint_email"). innerhtml= ""; } }

Code Explanation:

STR is the contents of the input box, ID is the ID name of the input box, ID is used to determine which input box to verify, such as:

if (id== "Contact_username") {

is the user name input box;

Pass parameter interpretation:

Xmlhttp.open ("GET", "..") /common/verify.php?v= "+str+" &id= "+id,true);

Through STR, ID to the user name and input box ID to the server, with PHP code to verify;

3.php Code:

(user account, and password Authentication)

<?php//Registration Verification----------$v = Trim ($_get[' V ');   Gets the information entered by the user $id = Trim ($_get[' id ');  Gets the ID used to determine what authentication $hint = ""; Used as the return output//Determine whether the account or password, or other matching if ($id = = "Contact_username") {//Account verification//determine the input account length is greater than 0 if (strlen ($v) > 0) {//  User authentication//1. Must start with a letter if (Preg_match ("/^[a-z]/", $v)) {//2. At least 5 characters maximum of 11 characters if (strlen ($v) < 5 | |            strlen ($v) > One) {$hint = "At least 5 characters, maximum no more than 11 characters!";                    } else {//3. Pattern matches if (Preg_match ("/^[a-z][\w]{2,10}$/", $v)) {echo $v;  $hint = "";                    When satisfied, let it enter empty because the front does not meet the assigned value//database to establish a connection require "mysqli.php";                    Database query statement--Query the input account exists $sql = "Select ' username ' from ' user ' where ' username ' = ' $v '";                    $result = Mysqli_query ($conn, $sql);     When Mysqli_num_rows ($result) > 0 description found Data if (Mysqli_num_rows ($result) > 0) {                   $hint = "The user already exists!"                    ";                    } else {$hint = "This user is available"; } mysqli_close ($conn);                Close database Connection} else {$hint = "user name can only be numeric, letter, underline"; }}}} else {$hint = "must start with a letter!        ";        }}} else if ($id = = "Contact_password") {//Password verify//Determine if the password entered is greater than 0 if (strlen ($v) > 0) {//1. Must start with a letter if (Preg_match ("/^[a-z]/", $v)) {//2. At least 8 characters maximum of 16 characters if (strlen ($v) < 8 | | strlen ($V) > 1            1) {$hint = "password at least 8 characters, maximum no more than 16 characters!"; } else {//3. Pattern matches if (Preg_match ("/^[a-z][\w]{2,16}$/", $v)) {$hint = "secret                Code available ";                } else {$hint = "password can only be numeric, letter, underline"; }}}} else {$hint = "must start with a letter!        "; }    }

Confirm Password Verification:

} else if ($id = = "Contact_password_") {  //Confirm password    //Because it is the confirmation password, only need to determine and the last password is the same

As you can see, we just have to decide if it's the same as the last password, and I need to create a data that stores temporary variables.

However, this PHP file is constantly updated, so create a record with the database to save temporary data,

As follows:


This record is then constantly updated:

$sql = "Update ' user ' set ' password ' = ' $pass ' where ' id ' = 1";

So, the new password is verified:

if ($id = = "Contact_password") {//Password verify//Determine if the password entered is greater than 0 if (strlen ($v) > 0) {//1. Must start with a letter if (preg_m                Atch ("/^[a-z]/", $v)) {//2. At least 8 characters maximum of 16 characters if (strlen ($v) < 8 | | strlen ($V) > 16) {            $hint = "Password at least 8 characters, maximum of 16 characters!"; } else {//3. Pattern matches if (Preg_match ("/^[a-z][\w]{2,16}$/", $v)) {//When the password is available, I                   Passwords are MD5 encrypted 2 times $pass = MD5 (MD5 ($V));                    Save to database require "mysqli.php";                    Because the database exists, so only need to update to be able//$sql = "INSERT into user (' password ', ' id ', ' username ') value (' $v ', 1, ' MMJC ')";                    $sql = "Update ' user ' set ' password ' = ' $pass ' where ' id ' = 1";                    if (Mysqli_query ($conn, $sql)) {$hint = "password available";                } mysqli_close ($conn);                } else {$hint = "password can only be numeric, letter, underline";     }       }} else {$hint = "must start with a letter!        "; }    }}

Code Explanation:

Through the database UPDATE statement, the real-time update of the data to achieve dynamic verification.

$sql = "Update ' user ' set ' password ' = ' $pass ' where ' id ' = 1";

Then the confirmation password is verified!

PHP Code:

if ($id = = "Contact_password_") {  //Confirm password     //When the password is available, we encrypt the password 2 times MD5     $pass = MD5 (MD5 ($V));    Query the password of the first input password     require "mysqli.php";    Database query statement--whether the query password and the first password is     the same $sql = "Select ' Password ' from ' user ' where id=1";     $result = Mysqli_query ($conn, $sql);     if (Mysqli_num_rows ($result) > 0) {        //output data         $row = MYSQLI_FETCH_ASSOC ($result);         Determine if two passwords are the same         if ($pass = = $row ["password"]) {             $hint = "two times the same password is available";         } else{             $hint = "Please keep the same password as before!" ";         } }     Mysqli_close ($conn);}

Code Explanation:

Because it is a confirmation of two times the password, I get the temporary record in the database directly, and the user input comparison (using the database query statement).

After the user password verification is complete, the mobile phone number and the mailbox verification:

Mobile phone number verification:

if ($id = = "Contact_phone") {    //1. Mobile number must start with 1    if (Preg_match ("/^[1]/", $v)) {        if (strlen ($v)! =) {            $ hint = "mobile phone number is 11 bits";        } else if (Preg_match ("/^[1][0-9]{10}$/", $v)) {            require "mysqli.php";            Query the database for existing mobile phone number            $sql = "Select  ' phone ' from ' user ' where ' phone ' = ' $v '";            $result = Mysqli_query ($conn, $sql);            if (Mysqli_num_rows ($result) > 0) {                $row = Mysqli_fetch_assoc ($result);                The user entered the password is the same as the database inside the same                if ($v = = $row ["Phone"] {                    $hint = "The phone has been registered! ";                } else {                    $hint = "mobile number available";                }            }            Mysqli_close ($conn);        } else{            $hint = "Mobile phone number must be a number!" ";        }    } else {        $hint = "Mobile number must start with 1! ";    }}

Limited validation:

i F ($id = = "Contact_email") {$email _pattern = "/^[\w]+ (\.[ \w]+) *@[a-z0-9]+ (\.[    a-z0-9]+) +$/";    if (Preg_match ($email _pattern, $v)) {$hint = "legal mailbox, available";    }else {$hint = "The mailbox you entered is not valid"; }

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.