删除PHP Cookie? [英] Delete PHP Cookie?

查看:80
本文介绍了删除PHP Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个cookie设置如下:

I currently have a cookie set as follows:

setcookie("username",$username,time()+3600*24*5);

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

How would I go about clearing the value of that cookie so that the user's username isn't filled in anymore?

我已经清除了以下内容:

I have it cleared as follows:

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

尽管如此,用户的用户名仍然出现.

The user's username still comes up though.

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脚本:

The PHP script to handle the form:

<?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",$username,time()+3600*24*5,'','.ohjustthatguy.com');
            setcookie("password",$password,time()+3600*24*2,'','.ohjustthatguy.com');
        } else {
            setcookie("username","",time()-10,'','.ohjustthatguy.com');
            setcookie("password","",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的原始来源

Original sources on 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的路径,以便它们也可以在您的子域之间共享.

Be sure that you delete the cookie with the same domain name and path with which you set it. Cookies for example.com and www.example.com will be treated as two different cookies. Similarly, cookies set from example.com and example.com/Support will have different paths. A good practice is to use .example.com as the domain and '/' as the path for username type cookies so that they can be shared across your subdomains too.

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

To debug this, you can use the FireCookie plugin of Firefox which'll show all this information.

这篇关于删除PHP Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆