删除PHP Cookie?

前端之家收集整理的这篇文章主要介绍了删除PHP Cookie?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前有一个cookie设置如下:
setcookie("username",$username,time()+3600*24*5);

我将如何清除该cookie的值,以便不再填写用户用户名

我把它清除如下:

setcookie("username","",time()-60000);

用户用户名仍然会出现.

HTML表单:

<?PHP
    session_start();

    $username = NULL;
    $password = NULL;

    if(isset($_SESSION['username'])){
        $username = $_COOKIE['username'];
        $password = $_COOKIE['password'];
    }
?>
<html>
    <title>Login</title>
    <body bgcolor='#000000'>
        <font color="white">
    <H2><div align='center'>Login</div></H2>
    <form align='center' action='login.PHP' method='POST'>
            Username: <input type='text' name='username' value='<?$_COOKIE['username']?>'><br \>
            Password: <input type='password' name='password' value='<?$password?>'><br \>
            Remember Me <input type='checkBox' name='remember' value='rememberme'><br \>
            <input type='submit' value='Login'>
        </form>
        </font>
    </body>
</html>

用于处理表单的PHP脚本:

<?PHP
    session_start();

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

    //Hash password in a new variable
    $password2 = md5($password);

    require_once "/home/a7435766/public_html/scripts/dbconnect.PHP";

    $query = MysqL_query("SELECT * FROM userstwo WHERE username = '$username' && password = '$password2'");

    if((MysqL_num_rows($query)) != 0) {
        //Store username and password in a cookie
        if($_POST['remember'] == 'rememberme') {
            setcookie("username",time()+3600*24*5,'','.ohjustthatguy.com');
            setcookie("password",$password,time()+3600*24*2,'.ohjustthatguy.com');
        } else {
            setcookie("username",time()-10,'.ohjustthatguy.com');
    }
        $_SESSION['username'] = $username;
        header('Location: http://www.ohjustthatguy.com/uploads/uploads.html');
        } else {
        header('Location: http://www.ohjustthatguy.com/uploads/');
    }
?>

关于pastebin的原始资料来源

> http://pastebin.com/8XtqV1PP
> http://pastebin.com/7GvQ9wRa

请确保删除具有相同域名和路径的cookie. example.com和www.example.com的Cookie将被视为两个不同的Cookie.同样,example.com和example.com/Support中设置的cookie将具有不同的路径.一个好的做法是使用.example.com作为域,使用’/’作为用户名类型cookie的路径,以便它们也可以在您的子域之间共享.

要对此进行调试,您可以使用Firefox的FireCookie插件显示所有这些信息.

猜你在找的PHP相关文章