PHP - Cookie 应用

效果:

 

代码:

<?php
    //设置编码
    header("content-type:text/html; charset=utf-8");

    //接收提交的数据
    //判断是否接受提交了表单
    if (isset($_POST['submit'])) {
        $userarray = array();
        $userarray['useraccount'] = isset($_POST['useraccount']) ? $_POST['useraccount'] : null;
        $userarray['userpassword'] = isset($_POST['userpassword']) ? $_POST['userpassword'] : null;
        $ismemory = isset($_POST['ismemory']) ? true : null;
        
        //判断需要参数是否为空
        foreach ($userarray as $user) {
            if ($user == null) {
                echo '参数为空!';
                exit();
            }
        }
        
        //验证账号密码
        if ($userarray[''] == '123456' && $userarray[''] == '123456') {
            //判断是否存储账户
            if ($ismemory) {
                //存储cookie
                //长期有效
                setcookie('useraccount',$userarray['useraccount']);
            }
            
            //登录成功,跳转页面
            header('location:http://www.baidu.com');
        }
    }
?>
<html>
    <head>
        <title>登录页面</title>
    </head>
    <body>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <table>
                <tr>
                    <td>账号:</td>
                    <td><input type="text" name="useraccount" value="<?php echo $_COOKIE['useraccount']; ?>"></td>
                </tr>
                <tr>
                    <td>密码:</td>
                    <td><input type="password" name="userpassword"></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="checkbox" name="ismemory">是否记住账号?</td>
                </tr>
                 <tr>
                    <td></td>
                    <td><input type="submit" name="submit" value="登录"></td>
                </tr>
            </table>
        </form>
    </body>
</html>

 

posted on 2016-01-01 17:06  ultrastrong  阅读(191)  评论(0编辑  收藏  举报