> php教程 > php手册 > PHP 인증 사용자 로그인 예제-학습 노트

PHP 인증 사용자 로그인 예제-학습 노트

WBOY
풀어 주다: 2016-11-30 23:59:38
원래의
1555명이 탐색했습니다.

1. 기본 과정:

2.UML 클래스 다이어그램:

3.PHP 코드:

3.1 index.php

<?<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * Created by PhpStorm.
 * User: andy
 * Date: 16-11-25
 * Time: 下午10:13
 </span><span style="color: #008000;">*/</span>

<span style="color: #008080;">session_start</span><span style="color: #000000;">();
</span><span style="color: #800080;">$validate_username</span>=<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_SESSION</span>['validate_username'])?<span style="color: #800080;">$_SESSION</span>['validate_username']:''<span style="color: #000000;">;
</span><span style="color: #800080;">$validate_password</span>=<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_SESSION</span>['validate_password'])?<span style="color: #800080;">$_SESSION</span>['validate_password']:''<span style="color: #000000;">;

</span>?>

<html>
<head>
    <meta charset="utf-8"/>
    <title>用户登录</title>
</head>
<body>
<h1>用户登录</h1>
<form action="login.php" method="post"><span style="color: #000000;">
    用户名:</span><input type="text" name="username" value="" /><font color="red">
        <?php  <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$validate_username</span>; ?>
    </font><br /><br /><span style="color: #000000;">
    密</span>   码:<input type="password" name="password" value="" /><font color="red">
        <?php  <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$validate_password</span>; ?>
    </font><br /><br />
    <input type="submit" value="提交" />
</form>
</body>
</html>
로그인 후 복사
코드 보기

3.2 login.php

<?<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * Created by PhpStorm.
 * User: andy
 * Date: 16-11-25
 * Time: 下午10:20
 </span><span style="color: #008000;">*/</span>

<span style="color: #008080;">session_start</span><span style="color: #000000;">();

</span><span style="color: #800080;">$username</span> = <span style="color: #800080;">$_POST</span>['username'<span style="color: #000000;">];
</span><span style="color: #800080;">$password</span> = <span style="color: #800080;">$_POST</span>['password'<span style="color: #000000;">];


</span><span style="color: #800080;">$user</span> = <span style="color: #0000ff;">new</span> User(<span style="color: #800080;">$username</span>, <span style="color: #800080;">$password</span><span style="color: #000000;">);

</span><span style="color: #008000;">//</span><span style="color: #008000;">判断登录是否成功</span>
<span style="color: #0000ff;">try</span><span style="color: #000000;">{
    Validate</span>::validateUser(<span style="color: #800080;">$user</span><span style="color: #000000;">);

    </span><span style="color: #008000;">//</span><span style="color: #008000;">验证通过,登录成功</span>
    <span style="color: #800080;">$_SESSION</span>['username']=<span style="color: #800080;">$username</span><span style="color: #000000;">;
    </span><span style="color: #008080;">header</span>('location:main.php'<span style="color: #000000;">);
}</span><span style="color: #0000ff;">catch</span> (MyException <span style="color: #800080;">$me</span><span style="color: #000000;">){

    </span><span style="color: #008000;">//</span><span style="color: #008000;">验证失败</span>
    <span style="color: #008080;">header</span>('location:index.php'<span style="color: #000000;">);
}


</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 自动加载类
 * @param $class
 * @return string
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">function</span> __autoload(<span style="color: #800080;">$class</span><span style="color: #000000;">)
{
    </span><span style="color: #800080;">$file</span> = __DIR__ . '/' . <span style="color: #008080;">strtolower</span>(<span style="color: #800080;">$class</span>) . '.php'<span style="color: #000000;">;
    </span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">is_file</span>(<span style="color: #800080;">$file</span><span style="color: #000000;">)) {
        </span><span style="color: #0000ff;">include_once</span> <span style="color: #800080;">$file</span><span style="color: #000000;">;
    }
    </span><span style="color: #0000ff;">return</span> ''<span style="color: #000000;">;
}</span>
로그인 후 복사
코드 보기

3.3 myException.php

<?<span style="color: #000000;">php

</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * Created by PhpStorm.
 * User: andy
 * Date: 16-11-25
 * Time: 下午10:50
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">class</span> MyException <span style="color: #0000ff;">extends</span> <span style="color: #0000ff;">Exception</span><span style="color: #000000;">
{

}</span>
로그인 후 복사
코드 보기

3.4 user.php

<?<span style="color: #000000;">php

</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * Created by PhpStorm.
 * User: andy
 * Date: 16-11-25
 * Time: 下午10:29
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">class</span><span style="color: #000000;"> User
{
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$username</span> = ''<span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$password</span> = ''<span style="color: #000000;">;

    </span><span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$username</span>, <span style="color: #800080;">$password</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$this</span>->username = <span style="color: #800080;">$username</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$this</span>->password = <span style="color: #800080;">$password</span><span style="color: #000000;">;
    }

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 返回用户名
     * @return string
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getUsername(): <span style="color: #0000ff;">string</span><span style="color: #000000;">
    {
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">username;
    }

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 返回密码
     * @return string
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getPassword(): <span style="color: #0000ff;">string</span><span style="color: #000000;">
    {
        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">password;
    }
}</span>
로그인 후 복사
코드 보기

3.5 verify.php

<?<span style="color: #000000;">php

</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 验证类
 * Created by PhpStorm.
 * User: andy
 * Date: 16-11-25
 * Time: 下午10:34
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">class</span><span style="color: #000000;">  Validate
{
    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 验证用户
     * @param User $user
     * @throws MyException
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> validateUser(User <span style="color: #800080;">$user</span><span style="color: #000000;">)
    {
        </span><span style="color: #008000;">//</span><span style="color: #008000;">print_r($user);</span>
        <span style="color: #800080;">$username</span> = <span style="color: #800080;">$user</span>-><span style="color: #000000;">getUsername();
        </span><span style="color: #800080;">$password</span> = <span style="color: #800080;">$user</span>-><span style="color: #000000;">getPassword();

        </span><span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$_SESSION</span>['validate_username'],<span style="color: #800080;">$_SESSION</span>['validate_password'<span style="color: #000000;">]);

        </span><span style="color: #008000;">//</span><span style="color: #008000;">验证用户名</span>
        <span style="color: #0000ff;">try</span><span style="color: #000000;"> {
            self</span>::validateUsername(<span style="color: #800080;">$username</span><span style="color: #000000;">);
        }</span><span style="color: #0000ff;">catch</span> (MyException <span style="color: #800080;">$me</span><span style="color: #000000;">) {
            </span><span style="color: #800080;">$_SESSION</span>['validate_username']=<span style="color: #800080;">$me</span>-><span style="color: #000000;">getMessage();
        }

        </span><span style="color: #008000;">//</span><span style="color: #008000;">验证密码</span>
        <span style="color: #0000ff;">try</span><span style="color: #000000;"> {
            self</span>::validatePassword(<span style="color: #800080;">$password</span><span style="color: #000000;">);
        }</span><span style="color: #0000ff;">catch</span> (MyException <span style="color: #800080;">$me</span><span style="color: #000000;">) {
            </span><span style="color: #800080;">$_SESSION</span>['validate_password']=<span style="color: #800080;">$me</span>-><span style="color: #000000;">getMessage();
        }


        </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$me</span><span style="color: #000000;">)){
            </span><span style="color: #0000ff;">throw</span> <span style="color: #800080;">$me</span><span style="color: #000000;">;
        }

    }

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 验证用户名
     * @param $username
     * @throws MyException
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> validateUsername(<span style="color: #800080;">$username</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$lem</span> = <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$username</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$lem</span> < 3<span style="color: #000000;">) {
            </span><span style="color: #008000;">//</span><span style="color: #008000;">抛出异常</span>
            <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> MyException('用户名长度不能小于3位', <span style="color: #ff00ff;">E_USER_ERROR</span><span style="color: #000000;">);
        } </span><span style="color: #0000ff;">elseif</span> (<span style="color: #800080;">$lem</span> > 8<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> MyException('用户名长度不能超过8位', <span style="color: #ff00ff;">E_USER_ERROR</span><span style="color: #000000;">);
        }
    }

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     * 验证密码
     * @param $password
     * @throws MyException
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> validatePassword(<span style="color: #800080;">$password</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$lem</span> = <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$password</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$lem</span> < 3<span style="color: #000000;">) {
            </span><span style="color: #008000;">//</span><span style="color: #008000;">抛出异常</span>
            <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> MyException('密码长度不能小于3位', <span style="color: #ff00ff;">E_USER_ERROR</span><span style="color: #000000;">);
        } </span><span style="color: #0000ff;">elseif</span> (<span style="color: #800080;">$lem</span> > 8<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> MyException('密码长度不能超过8位', <span style="color: #ff00ff;">E_USER_ERROR</span><span style="color: #000000;">);
        }
    }

}</span>
로그인 후 복사
코드 보기

(끝)

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿