Home  >  Article  >  php教程  >  PHP authentication user login example-study notes

PHP authentication user login example-study notes

WBOY
WBOYOriginal
2016-11-30 23:59:381422browse

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: 下午10:13
 */

session_start();
$validate_username=isset($_SESSION['validate_username'])?$_SESSION['validate_username']:'';
$validate_password=isset($_SESSION['validate_password'])?$_SESSION['validate_password']:'';

?>



    
    用户登录

用户登录

用户名: echo $validate_username; ?>

码: echo $validate_password; ?>

View Code

3.2 login.php

php
/**
 * Created by PhpStorm.
 * User: andy
 * Date: 16-11-25
 * Time: 下午10:20
 */

session_start();

$username = $_POST['username'];
$password = $_POST['password'];


$user = new User($username, $password);

//判断登录是否成功
try{
    Validate::validateUser($user);

    //验证通过,登录成功
    $_SESSION['username']=$username;
    header('location:main.php');
}catch (MyException $me){

    //验证失败
    header('location:index.php');
}


/**
 * 自动加载类
 * @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: 下午10:50
 */
class MyException extends Exception
{

}
View Code

3.4 user.php

php

/**
 * Created by PhpStorm.
 * User: andy
 * Date: 16-11-25
 * Time: 下午10:29
 */
class User
{
    private $username = '';
    private $password = '';

    function __construct($username, $password)
    {
        $this->username = $username;
        $this->password = $password;
    }

    /**
     * 返回用户名
     * @return string
     */
    public function getUsername(): string
    {
        return $this->username;
    }

    /**
     * 返回密码
     * @return string
     */
    public function getPassword(): string
    {
        return $this->password;
    }
}
View Code

3.5 validate.php

php

/**
 * 验证类
 * Created by PhpStorm.
 * User: andy
 * Date: 16-11-25
 * Time: 下午10:34
 */
class  Validate
{
    /**
     * 验证用户
     * @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']);

        //验证用户名
        try {
            self::validateUsername($username);
        }catch (MyException $me) {
            $_SESSION['validate_username']=$me->getMessage();
        }

        //验证密码
        try {
            self::validatePassword($password);
        }catch (MyException $me) {
            $_SESSION['validate_password']=$me->getMessage();
        }


        if(isset($me)){
            throw $me;
        }

    }

    /**
     * 验证用户名
     * @param $username
     * @throws MyException
     */
    static private function validateUsername($username)
    {
        $lem = strlen($username);
        if ($lem < 3) {
            //抛出异常
            throw new MyException('用户名长度不能小于3位', E_USER_ERROR);
        } elseif ($lem > 8) {
            throw new MyException('用户名长度不能超过8位', E_USER_ERROR);
        }
    }

    /**
     * 验证密码
     * @param $password
     * @throws MyException
     */
    static private function validatePassword($password)
    {
        $lem = strlen($password);
        if ($lem < 3) {
            //抛出异常
            throw new MyException('密码长度不能小于3位', E_USER_ERROR);
        } elseif ($lem > 8) {
            throw new MyException('密码长度不能超过8位', E_USER_ERROR);
        }
    }

}
View Code

(End.)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn