Implementation of input information validators during php Registration

Source: Internet
Author: User

1. Type of input information verification (mainly used to verify the user name, password, repeated password, email address, and other functions)
Copy codeThe Code is as follows: <? Php
/**
* 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
Copy codeThe 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 (). "<br> ";
}

3. The Error class in the code is used to record the Error information during verification.
Copy codeThe Code is as follows: <? Php
/**
* 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;
}
}
?>

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.