1. Basic process:
2.UML class Diagram:
3.PHP Code:
3.1 index.php
<?PHP/** * Created by Phpstorm. * User:andy * date:16-11-25 * Time: PM 10:13*/Session_Start();$validate _username=isset($_session[' Validate_username '])?$_session[' Validate_username ']: ';$validate _password=isset($_session[' Validate_password '])?$_session[' Validate_password ']: ';?>User name:<input type= "text" name= "username" value= ""/><font color= "Red" > <?phpEcho $validate _username;?> </font><br/><br/>Dense code: <input type= "password" name= "password" value= ""/><font color= "Red" > &L t;? PhpEcho $validate _password;?> </font><br/><br/> <input type= "Submit" value= "Submit"/></form></body>< /html>
View Code
3.2 login.php
<?PHP/** * Created by Phpstorm. * User:andy * date:16-11-25 * Time: PM 10:20*/Session_Start();$username=$_post[' username '];$password=$_post[' Password '];$user=NewUser ($username,$password);//determine if the login was successfulTry{Validate:: ValidateUser ($user); //verification passed, login successful $_session[' username ']=$username; Header(' location:main.php ');}Catch(myexception$me){ //validation Failed Header(' location:index.php ');}/** * Automatically load class * @param $class * @return string*/function__autoload ($class){ $file= __dir__. ‘/‘ .Strtolower($class) . '. php '; if(Is_file($file)) { include_once $file; } return‘‘;}
View Code
3.3 myexception.php
<? PHP /* */classextendsException{}
View Code
3.4 user.php
<?PHP/** * Created by Phpstorm. * User:andy * date:16-11-25 * Time: PM 10:29*/classuser{Private $username= ' '; Private $password= ' '; function__construct ($username,$password) { $this->username =$username; $this->password =$password; } /** * Return user name * @return string*/ Public functionGetUserName ():string { return $this-username; } /** * Return password * @return string*/ Public functionGetPassword ():string { return $this-password; }}
View Code
3.5 validate.php
<?PHP/** * Verify class * Created by Phpstorm. * User:andy * date:16-11-25 * Time: PM 10:34*/classvalidate{/** * Verify user * @param user $user * @throws myexception*/ Static functionValidateUser (User$user) { //Print_r ($user); $username=$user-GetUserName (); $password=$user-GetPassword (); unset($_session[' Validate_username '],$_session[' Validate_password ']); //Verify user name Try{ Self:: Validateusername ($username); }Catch(myexception$me) { $_session[' Validate_username ']=$me-GetMessage (); } //Verify Password Try{ Self:: ValidatePassword ($password); }Catch(myexception$me) { $_session[' Validate_password ']=$me-GetMessage (); } if(isset($me)){ Throw $me; } } /** * Verify username * @param $username * @throws myexception*/ Static Private functionValidateusername ($username) { $lem=strlen($username); if($lem< 3) { //Throw Exception Throw NewMyException (' User name length cannot be less than 3 bits ',E_user_error); } ElseIf($lem> 8) { Throw NewMyException (' username cannot be longer than 8 bits ',E_user_error); } } /** * Verify password * @param $password * @throws myexception*/ Static Private functionValidatePassword ($password) { $lem=strlen($password); if($lem< 3) { //Throw Exception Throw NewMyException (' Password length cannot be less than 3 bits ',E_user_error); } ElseIf($lem> 8) { Throw NewMyException (' Password length cannot exceed 8 bits ',E_user_error); } }}
View Code
(end.)
PHP Authentication User Login Example-Learning notes