Home > Article > Backend Development > PHP login username and password verification program code_PHP tutorial
A PHP username and password verification example program for login, friends who need to learn can refer to it.
, Verify the username and password entered by the user when logging in
The code is as follows | Copy code | ||||
<🎜>/** * Validator for Login. */ final class LoginValidator {<🎜> <🎜> private function __construct() { }<🎜> <🎜> /** * Validate the given username and password. * @param $username and $password to be validated * @return array array of {@link Error} s */ Public static function validate($username, $password) { $errors = array(); $username = trim($username); if (!$username) { $errors[] = new Error('username', 'Username cannot be empty.'); } elseif (strlen($username)<3) { $errors[] = new Error('username', 'The username cannot be less than 3 characters in length.'); } elseif (strlen($username)>30) { $errors[] = new Error('username', 'The username cannot exceed 30 characters in length.'); } 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', 'The username can only be a combination of letters, numbers and underscores (_).'); } elseif (!trim($password)) { $errors[] = new Error('password', 'Password cannot be empty.'); } else { // check whether use exists or not $dao = new UserDao(); $user = $dao->findByName($username); if ($user) { If (!($user->getPassword() == sha1($user->getSalt() . $password))) { } } else { $errors[] = new Error('username', 'Username does not exist.'); } } return $errors; } } ?> |
2、调用验证器进行验证
代码如下
|
复制代码 | ||||
$msg = "";
www.bkjia.com