PHP验证用户登录例子-学习笔记

2018-06-22 05:01:37来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

1.基本流程:

 

2.UML类图:

 

3.PHP代码:

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']:'';

?>

<html>
<head>
    <meta charset="utf-8"/>
    <title>用户登录</title>
</head>
<body>
<h1>用户登录</h1>
<form action="login.php" method="post">
    用户名:<input type="text" name="username" value="" /><font color="red">
        <?php  echo $validate_username; ?>
    </font><br /><br />&nbsp;&nbsp;&nbsp;码:<input type="password" name="password" value="" /><font color="red">
        <?php  echo $validate_password; ?>
    </font><br /><br />
    <input type="submit" value="提交" />
</form>
</body>
</html>
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

 

(完.)

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:FusionCharts的使用方法 - 公司所用的flash式的图像统计工具

下一篇:dede织梦cms-dede:autochannel标签