Implementation of input information validators during php Registration _ PHP Tutorial

Source: Internet
Author: User
Implementation details of the input information validators during php registration. 1. Copy the following code to verify the input information (mainly used to verify the user name, password, repeated password, email address, and other functions :? Php *** Validatorfor 1. type of input information verification (mainly used to verify the user name, password, repeated password, email address, and other functions)

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, registration failed due to internal server error. 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:


/**
* Validation error.
*/
Final class Error {
Private $ source;
Private $ message;
/**
* Create new error.
* @ Param mixed $ source of the error
* @ Param string $ message error message
*/
Function _ construct ($ source, $ message ){
$ This-> source = $ source;
$ This-> message = $ message;
}
/**
* Get source of the error.
* @ Return mixed source of the error
*/
Public function getSource (){
Return $ this-> source;
}
/**
* Get error message.
* @ Return string error message
*/
Public function getMessage (){
Return $ this-> message;
}
}
?>

The following code verifies the username, password, repeated password, and email address :? Php/*** Validator...

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.