The implementation of input information authenticator when PHP registration _php tutorial

Source: Internet
Author: User
Tags sha1
1, the input information to verify the class (mainly used to verify the user name, password, duplicate password, mailbox, can add other functions)
Copy CodeThe 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 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 ', ' username cannot be empty. ');
} elseif (Strlen ($username) <3) {
$errors [] = new Error (' username ', ' username cannot be less than 3 characters long. ');
} elseif (Strlen ($username) >30) {
$errors [] = new Error (' username ', ' username cannot be longer than 30 characters. ');
} elseif (!preg_match ('/^[a-za-z]+$/', substr ($username, 0, 1))) {
$errors [] = new Error (' username ', ' username must start with a letter. ');
} elseif (!preg_match ('/^[a-za-z0-9_]+$/', $username)) {
$errors [] = new Error (' username ', ' username can only be a combination of letters, numbers, and underscores (_). ');
} elseif (! $password) {
$errors [] = new Error (' Password ', ' password cannot be empty. ');
} elseif (Strlen ($password) <6) {
$errors [] = new Error (' Password ', ' password length cannot be less than 6 characters. ');
} elseif (Strlen ($password) >30) {
$errors [] = new Error (' Password ', ' password length cannot exceed 30 characters. ');
} elseif (!preg_match ('/^[a-za-z0-9!@#\\$%\\^&\\*_]+$/', $password)) {
$errors [] = new Error (' Password ', ' password can only be a combination of characters such as numbers, letters, or!@#$%^&*_. ');
} elseif ($password! = Trim ($repeat _password)) {
$errors [] = new Error (' Password ', ' two times the input password is inconsistent. ');
} elseif (! Utils::isvalidemail ($email)) {
$errors [] = new Error (' email ', ' mailbox format is incorrect. ');
} else {
Check whether user exists or not
$dao = new Userdao ();
$user = $dao->findbyname (Trim ($username));
if ($user) {
$errors [] = new Error (' username ', ' the user name is already in use. ');
}

$user = null;
Check whether email being used or not
$user = $dao->findbyemail (Trim ($email));
if ($user) {
$errors [] = new Error (' email ', ' the mailbox has been registered. ');
}
}
return $errors;
}
}
?>

2. Make a 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, the registration failed due to a server internal error. Please try again later. ');
}
Utils::redirect (' welcome ');
}

foreach ($errors as $e) {
$msg. = $e->getmessage (). "
";
}

3. Error message in code to log validation
Copy CodeThe code is as follows:
/**
* Validation error.
*/
Final class Error {
Private $source;
Private $message;
/**
* Create new error.
* @param mixed $source 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;
}
}
?>

http://www.bkjia.com/PHPjc/327996.html www.bkjia.com true http://www.bkjia.com/PHPjc/327996.html techarticle 1, the input information to verify the class (mainly used to verify the user name, password, duplicate password, mailbox, can add other functions) copy the code as follows:? PHP/** * Validator for ...

  • 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.