Implementation of input information validators during php Registration

Source: Internet
Author: User
This article provides a detailed analysis of the implementation method of the input information validators during php registration. For more information, see

This article provides a detailed analysis of the implementation method of the input information validators during php registration. For more information, see

1. Type of verification of input information (mainly used to verify the user name, Hong Kong server, password, repeated password, Hong Kong Space, mailbox, other functions can be added)

The Code is as follows:


/**
* Validator for Register.
*/
Final class RegisterValidator {
Private function _ construct (){

}
/**
* Validate the given username, password, repeat_password and email.
* @ Param $ username, $ password, $ repeat_password and $ email to be validated
* @ Return array of {@ link Error} s
*/
Public static function validate ($ username, $ password, $ repeat_password, $ email ){
$ Errors = array ();
$ Username = trim ($ username );
$ Password = trim ($ password );
If (! $ Username ){
$ Errors [] = new Error ('username', 'user name cannot be blank. ');
} Elseif (strlen ($ username) <3 ){
$ Errors [] = new Error ('username', 'user Name Length cannot be less than 3 characters. ');
} Elseif (strlen ($ username)> 30 ){
$ Errors [] = new Error ('username', 'the username length cannot exceed 30 characters. ');
} Elseif (! Preg_match ('/^ [A-Za-z] + $/', substr ($ username, 0, 1 ))){
$ Errors [] = new Error ('username', 'the user name must start with a letter. ');
} Elseif (! Preg_match ('/^ [A-Za-z0-9 _] + $/', $ username )){
$ Errors [] = new Error ('username', 'user name can only be a combination of letters, numbers, and underscores. ');
} Elseif (! $ Password ){
$ Errors [] = new Error ('Password', 'the password cannot be blank. ');
} Elseif (strlen ($ password) <6 ){
$ Errors [] = new Error ('Password', 'the password length cannot be less than 6 characters. ');
} Elseif (strlen ($ password)> 30 ){
$ Errors [] = new Error ('Password', 'the password length cannot exceed 30 characters. ');
} Elseif (! Preg_match ('/^ [A-Za-z0-9! @ # \\\ %\\^\\\ * _] + $/', $ Password )){
$ Errors [] = new Error ('Password', 'the password can only be a number, letter, or! @ # $ % ^ & * _ And other characters. ');
} Elseif ($ password! = Trim ($ repeat_password )){
$ Errors [] = new Error ('Password', 'the two passwords are inconsistent. ');
} Elseif (! Utils: isValidEmail ($ email )){
$ Errors [] = new Error ('email ',' the email format is incorrect. ');
} Else {
// Check whether user exists or not
$ Dao = new UserDao ();
$ User = $ dao-> findByName (trim ($ username ));
If ($ user ){
$ Errors [] = new Error ('username', 'this user name has been used. ');
}

$ User = null;
// Check whether email being used or not
$ User = $ dao-> findByEmail (trim ($ email ));
If ($ user ){
$ Errors [] = new Error ('email ',' this email address has been registered. ');
}
}
Return $ errors;
}
}
?>


2. Call on the registration page

The Code is as follows:


$ Username = null;
$ Password = null;
$ Repeat_password = null;
$ Email = null;
$ Msg = "";
If (isset ($ _ POST ['username']) & isset ($ _ POST ['Password'])
& Isset ($ _ POST ['Repeat _ password']) & isset ($ _ POST ['email ']) {
$ Username = addslashes (trim (stripslashes ($ _ POST ['username']);
$ Password = addslashes (trim (stripslashes ($ _ POST ['Password']);
$ Repeat_password = addslashes (trim (stripslashes ($ _ POST ['Repeat _ password']);
$ Email = addslashes (trim (stripslashes ($ _ POST ['email ']);
// Validate
$ Errors = RegisterValidator: validate ($ username, $ password, $ repeat_password, $ email );
// Validate
If (empty ($ errors )){
// Save
$ Dao = new UserDao ();
$ User = new User ();
$ User-> setEmail ($ email );
$ Last_login_ip = Utils: getIpAddress ();
$ User-> setLastLoginIp ($ last_login_ip );
$ User-> setUsername ($ username );
$ Salt = substr (sha1 (mt_rand (), 0, 22 );
$ Hash_password = sha1 ($ salt. $ password );
$ User-> setPassword ($ hash_password );
$ User-> setSalt ($ salt );
$ User = $ dao-> save ($ user );
If ($ user ){
UserLogin: setUserInfo ($ user );
Flash: addFlash ('registration successful! ');
}
Else {
Flash: addFlash ('Sorry, due to internal server error, the Hong Kong server is rented, resulting in registration failure. Please try again later. ');
}
Utils: redirect ('Welcome ');
}

Foreach ($ errors as $ e ){
$ Msg. = $ e-> getMessage ()."
";
}


3. The Error class in the code is used to record the Error information during verification.

The Code is as follows:

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.