学習リファレンス - Cookie


コード

ref: https://blog.csdn.net/u011781769/article/details/48470697

<?php
    if ($_COOKIE ['uname'] != '') {
        $CKUNAME = $_COOKIE ['uname'];
    }
    if ($_COOKIE ['pwd'] != '') {
        $CKPWD = $_COOKIE ['pwd'];
    }
    echo "\$CKUNAME:" . $CKUNAME;
    echo '<br>';
    echo "\$CKPWD:" . $CKPWD;
?>

<!DOCTYPE html>

<head>
    <title>Cookie</title>
    <h1> Cookie</h1>
</head>
<hr />
<body>

<form action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"])?>" method = "POST">
Name:   <input type="text" name="uname" id="uname" value="<?php echo $CKUNAME;?>"><br>
PW:    <input type="password" name="pwd" id="pwd" value="<?php echo $CKPWD;?>"><br>
    <input name="remember" type="checkbox" value="1" checked = <?php if($CKUNAME!=''){echo "checked";}?>> 记住我! 
    Debug : <?php echo "\$CKUNAME:" .  $CKUNAME?>
    <br>
    <input type="submit" name="button" id="button" value="登录" /> <br>
</form>
<hr />

<?php
    // 登录,将用户名和密码存入到COOKIE
    if ($_POST ['button'] != '') 
    {
        $uname = $_POST ['uname'];
        $pwd = $_POST ['pwd'];

        // 如果输入的加密密码和COOKIE中不一样,那么就加密
        if ($pwd != $CKPWD) {
            $pwd = md5 ( $pwd );
        }
        $remember = $_POST ['remember'];
        if ($remember == 1) {
            setcookie ( "uname", $uname, time () + 3600 * 24 * 30 );
            setcookie ( "pwd", $pwd, time () + 3600 * 24 * 30 );
        }
    }
?>
</body>

</html>