PHP Authentication User Login Example-Learning notes

Source: Internet
Author: User
Tags getmessage

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&nbsp;&nbsp;&nbsp; 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

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.