PHP verification User Logon example-learning notes, user logon learning notes
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: */session_start (); $ validate_username = isset ($ _ SESSION ['validate _ username'])? $ _ SESSION ['validate _ username']: ''; $ validate_password = isset ($ _ SESSION ['validate _ password'])? $ _ SESSION ['validate _ password']: '';?> <Html> 3.2 login. php
<? Php/*** Created by PhpStorm. * User: andy * Date: 16-11-25 * Time: */session_start (); $ username =$ _ POST ['username']; $ password = $ _ POST ['Password']; $ user = new User ($ username, $ password); // determine whether the logon is successful try {Validate :: validateUser ($ user); // the user is successfully authenticated. $ _ SESSION ['username'] = $ username; header ('location: main. php ');} catch (MyException $ me) {// header ('location: index. php ');}/*** automatically loaded 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/*** Created by PhpStorm. * User: andy * Date: 16-11-25 * Time: */class MyException extends Exception {}View Code
3.4 user. php
<? Php/*** Created by PhpStorm. * User: andy * Date: 16-11-25 * Time: */class User {private $ username = ''; private $ password = ''; function _ construct ($ username, $ password) {$ this-> username = $ username; $ this-> password = $ password ;} /*** return username * @ return string */public function getUsername (): string {return $ this-> username ;} /*** return password * @ return string */public function getPassword (): string {return $ this-> password ;}}View Code
3.5 validate. php
<? Php/*** verification class * Created by PhpStorm. * User: andy * Date: 16-11-25 * Time: am */class Validate {/*** authenticate User * @ param user $ User * @ throws MyException */static function validateUser (user $ User) {// print_r ($ user); $ username = $ user-> getUsername (); $ password = $ user-> getPassword (); unset ($ _ SESSION ['validate _ username'], $ _ SESSION ['validate _ password']); // verify the username try {self :: validateUsername ($ username);} catch (MyException $ me) {$ _ SESSION ['validate _ username'] = $ me-> getMessage ();} // verify the 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 function validateUsername ($ username) {$ lem = strlen ($ username ); if ($ lem <3) {// throw an exception throw new MyException ('username length cannot be less than 3 bits, E_USER_ERROR);} elseif ($ lem> 8) {throw new MyException ('username length cannot exceed 8 bits ', E_USER_ERROR );}} /*** verify the password * @ param $ password * @ throws MyException */static private function validatePassword ($ password) {$ lem = strlen ($ password ); if ($ lem <3) {// throw an exception throw new MyException ('password length cannot be less than 3 bits, E_USER_ERROR);} elseif ($ lem> 8) {throw new MyException ('password length cannot exceed 8 bits ', E_USER_ERROR );}}}View Code
(End .)