cookie不会设置 [英] cookie wont unset
问题描述
我无法取消设置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']); //
我做错了什么?
使用您的示例,我创建了此测试:
<? 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();
?>
在第一次加载时,它输出:
数组
pre>
(
)
数组
(
)
重新载入:
Array
b $ b [id] => 0001
[alias] => name
)
Array
(
[id] => 0001
[alias] => name
)
第二次重新载入:
数组
(
)
数组
(
)
因此,您的代码似乎在往返时重置了Cookie。
EDIT
以下内容:
<?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
(
)
或将打印:
数组
/ pre>
[id] => 0001
[alias] => name
)
Array
(
)
http://jfcoder.com/ test / cookies.php (重新载入几次)
如果您需要告诉浏览器忘记cookie,请使用
setcookie()
与时间设置的时间(我使用至少24小时)。如果您需要$ _ COOKIES
数组来忽略该值,请使用unset()
。
EDIT
这里有两个可能的问题,一个是cookie的子域不匹配,辅助功能问题。
例如...
如果访问的网址位于不同于尝试重置Cookie的网址,您需要将Cookie设置为允许该Cookie通过其他路径访问(和重置)的路径。
setcookie('my','cookie',time()+ 3600,'/');
或允许子目录中包含的路径...
setcookie('my','cookie',time()+ 3600,'/ my / path /');
如果访问的访问者访问的网址是一个子域(包括www),但您希望Cookie所有子域都可访问,您需要给setcookie一个通配符。
setcookie('my','cookie',time )+3600,'/','.example.com');
将允许来自www.example.com,my.example.com和sub.example的网址。 com访问和重置cookie。显然,在这一点上,你的路径考虑需要考虑,因为对于子域参数,你将需要包括一个路径。
/
在域选择子域之前选择网址上的所有子目录和。
(虽然子域'm不确定)。
http://php.net/manual/en/function.setcookie.php
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?
解决方案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, useunset()
.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
这篇关于cookie不会设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!