cookie 不会取消设置

发布于 2024-10-27 13:18:39 字数 438 浏览 5 评论 0原文

我无法取消设置 cookie。

cookie 设置:(id、别名)

setcookie("id",$data['id'], time()+3600*24*30);
setcookie("alias",$this->nombre, time()+3600*24*30);

cookie 未设置? (id,别名)

setcookie("id","-1",time()-315360000);
setcookie("alias","",time()-315360000);
unset($_COOKIE['id']);       // additional, but still no..
unset($_COOKIE['alias']);    //    "            "

我做错了什么?

I am unable to get the cookie to unset.

cookie set: (id, alias)

setcookie("id",$data['id'], time()+3600*24*30);
setcookie("alias",$this->nombre, time()+3600*24*30);

cookies unset? (id, alias)

setcookie("id","-1",time()-315360000);
setcookie("alias","",time()-315360000);
unset($_COOKIE['id']);       // additional, but still no..
unset($_COOKIE['alias']);    //    "            "

What I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可爱咩 2024-11-03 13:18:39

将时间设置为一小时前,而不是那么大的数字怎么样?

setcookie("alias", "", time()-3600);

How about setting the time to an hour back, rather than that large number?

setcookie("alias", "", time()-3600);
想你的星星会说话 2024-11-03 13:18:39

使用您的示例,我创建了此测试:

<?php

ob_start();

echo '<pre>';

setcookie("id","0001", time()+3600*24*30);
setcookie("alias","name", time()+3600*24*30);

print_r($_COOKIE);

if ($_COOKIE['id'] || $_COOKIE['alias']) {
    setcookie("id","-1",time()-315360000);
    setcookie("alias","",time()-315360000);
}

print_r($_COOKIE);

ob_end_flush();

?>

在第一次加载时,它输出:

Array
(
)
Array
(
)

在重新加载时:

Array
(
    [id] => 0001
    [alias] => name
)
Array
(
    [id] => 0001
    [alias] => name
)

在第二次重新加载时:

Array
(
)
Array
(
)

所以看来您的代码正在重置往返中的 cookie。

编辑

以下内容:

<?php

ob_start();

echo '<pre>';

setcookie("id","0001", time()+3600*24*30);
setcookie("alias","name", time()+3600*24*30);

print_r($_COOKIE);

if ($_COOKIE['id'] || $_COOKIE['alias']) {
    setcookie("id","-1",time()-315360000);
    setcookie("alias","",time()-315360000);
    unset($_COOKIE['id']);
    unset($_COOKIE['alias']);
}

print_r($_COOKIE);

ob_end_flush();

?>

将打印:

Array
(
)
Array
(
)

或将打印:

Array
(
    [id] => 0001
    [alias] => name
)
Array
(
)

http://jfcoder。 com/test/cookies.php(点击重新加载几次)

如果您需要告诉浏览器忘记 cookie,请使用 setcookie() 并及时设置时间(我至少使用24小时)。如果您需要 $_COOKIES 数组来忘记该值,请使用 unset()

编辑

这里可能存在两个问题,一是 cookie 上的子域不匹配,二是路径可访问性问题。

例如...

如果访问者访问的 url 与尝试重置 cookie 的 url 位于不同的目录中,则需要使用允许其他人访问(和重置)该 cookie 的路径来设置 cookie路径。

setcookie('my', 'cookie', time()+3600, '/');

或者允许子目录中包含的路径...

setcookie('my', 'cookie', time()+3600, '/my/path/');

如果访问者访问的 url 是子域(包括 www),但您希望所有子域都可以访问 cookie,则需要为 setcookie 提供通配符。

setcookie('my', 'cookie', time()+3600, '/', '.example.com');

将允许来自 www.example.com、my.example.com 和 sub.example.com 的 URL 访问和重置 cookie。显然,此时也需要考虑您的路径注意事项,因为对于子域参数,您将需要包含路径。 / 选择 url 上的所有子目录,而 . 在域之前选择子域(虽然 sub.sub.domains,我不确定)。

http://php.net/manual/en/function.setcookie.php

Using your example, I created this test:

<?php

ob_start();

echo '<pre>';

setcookie("id","0001", time()+3600*24*30);
setcookie("alias","name", time()+3600*24*30);

print_r($_COOKIE);

if ($_COOKIE['id'] || $_COOKIE['alias']) {
    setcookie("id","-1",time()-315360000);
    setcookie("alias","",time()-315360000);
}

print_r($_COOKIE);

ob_end_flush();

?>

On the first load, it outputs:

Array
(
)
Array
(
)

On reload:

Array
(
    [id] => 0001
    [alias] => name
)
Array
(
    [id] => 0001
    [alias] => name
)

On second reload:

Array
(
)
Array
(
)

So it appears your code is resetting the cookie on the roundtrip.

EDIT

The following:

<?php

ob_start();

echo '<pre>';

setcookie("id","0001", time()+3600*24*30);
setcookie("alias","name", time()+3600*24*30);

print_r($_COOKIE);

if ($_COOKIE['id'] || $_COOKIE['alias']) {
    setcookie("id","-1",time()-315360000);
    setcookie("alias","",time()-315360000);
    unset($_COOKIE['id']);
    unset($_COOKIE['alias']);
}

print_r($_COOKIE);

ob_end_flush();

?>

Will either print:

Array
(
)
Array
(
)

Or will print:

Array
(
    [id] => 0001
    [alias] => name
)
Array
(
)

http://jfcoder.com/test/cookies.php (hit reload a few times)

If you need to tell the browser to forget the cookie, use setcookie() with the time set back in time (I use at least 24 hours). If you need the $_COOKIES array to forget the value, use unset().

EDIT

There are two possible issues contributing here, one a subdomain mismatch on the cookie, and a path accessibility problem.

For instance...

If the url the visitor accessed was on a directory different from where the url that attempts to reset the cookie, you need to set the cookie with a path that will allow that cookie to be accessed (and reset) by other paths.

setcookie('my', 'cookie', time()+3600, '/');

Or to allow for paths contained within a subdirectory...

setcookie('my', 'cookie', time()+3600, '/my/path/');

If the url the visitor accessed was a subdomain (including www), but you want the cookie to be accessible to all subdomains, you need to give a wildcard to setcookie.

setcookie('my', 'cookie', time()+3600, '/', '.example.com');

Will allow urls from www.example.com, my.example.com, and sub.example.com to access and reset the cookie. Obviously, at this point too your path considerations need to be taken into account, since for a subdomain argument, you will need to include a path. / selects all subdirectories on the url, and . before the domain selects subdomains (although sub.sub.domains, I'm not sure).

http://php.net/manual/en/function.setcookie.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文