Username and password validators for php login
/**
- * 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;
- }
- }
- ?>
Error is a self-written class:
/**
- * 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;
- }
- }
- ?>
2. call the validators for verification.
$ 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 ()."
";
- }
- ?>
|