PHP verification User Logon example-learning notes, user logon learning notes

Source: Internet
Author: User

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 .)

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.