Implementation details of the Input information validator for php registration
- 2020-07-21 07:06:39
- OfStack
1. Class to verify input information (mainly used to verify user name, password, duplicate password, mailbox, other functions can be added)
2. Call in the registration page
3. The Error class in the code is used to record error messages during validation
<?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 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', ' The user name cannot be empty. ');
} elseif (strlen($username)<3) {
$errors[] = new Error('username', ' The username length cannot be less than 3 A character. ');
} elseif (strlen($username)>30) {
$errors[] = new Error('username', ' The username length cannot exceed 30 A character. ');
} elseif (!preg_match('/^[A-Za-z]+$/',substr($username, 0, 1))) {
$errors[] = new Error('username', ' The user name must begin with a letter. ');
} elseif (!preg_match('/^[A-Za-z0-9_]+$/', $username)) {
$errors[] = new Error('username', ' User names can only be letters, Numbers, and underscores ( _ ) The combination of. ');
} elseif (!$password) {
$errors[] = new Error('password', ' The password cannot be empty. ');
} elseif (strlen($password)<6) {
$errors[] = new Error('password', ' Password length cannot be less than 6 A character. ');
} elseif (strlen($password)>30) {
$errors[] = new Error('password', ' Password length cannot exceed 30 A character. ');
} elseif (!preg_match('/^[A-Za-z0-9!@#\\$%\\^&\\*_]+$/', $password)) {
$errors[] = new Error('password', ' Passwords can only be Numbers, letters, or !@#$%^&*_ A combination of equal characters. ');
} elseif ($password != trim($repeat_password)) {
$errors[] = new Error('password', ' Enter the password twice no 1 Cause. ');
} elseif (!Utils::isValidEmail($email)) {
$errors[] = new Error('email', ' The email is incorrectly formatted. ');
} 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', ' This mailbox has been registered. ');
}
}
return $errors;
}
}
?>
2. Call in the registration page
$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(' Registered! ');
}
else {
Flash::addFlash(' Sorry, registration failed due to an internal error on the server. 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 error messages during validation
<?php
/**
* 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;
}
}
?>