Php login username and password verification program code _ PHP Tutorial

Source: Internet
Author: User
Php login user name and password verification program code. A php user name and password verification instance program. For more information, see. The following code verifies the user name and password entered during logon.

The user name and password entered by the user are verified during logon.

The code is as follows:


/**
* Validator for Login.
*/
Final class LoginValidator {

Private function _ construct (){

}

/**
* Validate the given username and password.
* @ Param $ username and $ password to be validated
* @ Return array of {@ link Error} s
*/
Public static function validate ($ username, $ password ){
$ Errors = array ();
$ Username = trim ($ username );
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 (! Trim ($ password )){
$ Errors [] = new Error ('password', 'the password cannot be blank. ');
} Else {
// Check whether use exists or not
$ Dao = new UserDao ();
$ User = $ dao-> findByName ($ username );

If ($ user ){
If (! ($ User-> getPassword () = sha1 ($ user-> getSalt (). $ password ))){
$ Errors [] = new Error ('password', 'the user name or password is incorrect. ');
}
} Else {
$ Errors [] = new Error ('username', 'user name does not exist. ');
}
}
Return $ errors;
}
}

?>

2. call the validators for verification.

The code is as follows:


$ Username = null;
$ Password = null;

$ Msg = "";

If (isset ($ _ POST ['username']) & isset ($ _ POST ['password']) {
$ Username = addslashes (trim (stripslashes ($ _ POST ['username']);
$ Password = addslashes (trim (stripslashes ($ _ POST ['password']);
// Validate
$ Errors = LoginValidator: validate ($ username, $ password );

If (empty ($ errors )){
// Save the latest ip or login time into database, then processing page forwarding
$ Dao = new UserDao ();
$ User = $ dao-> findByName ($ username );
$ Last_login_ip = Utils: getIpAddress ();
$ User-> setLastLoginIp ($ last_login_ip );
$ Now = new DateTime ();
$ User-> setLastLoginTime ($ now );
$ Dao-> save ($ user );
UserLogin: setUserInfo ($ user );
Flash: addFlash ('login successful! ');
Utils: redirect ('Welcome ');
}

Foreach ($ errors as $ e ){
$ Msg. = $ e-> getMessage ()."
";
}

The user name and password used to authenticate the instance program when logging on to the supervisor. For more information, see. The user name and password entered by the user during login are verified as follows...

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.