[英]Set cookie not working after deleting previous cookie
I want to set a cookie if Username
is entered and also want the previous cookie to get deleted. 如果要输入
Username
我想设置一个cookie,也希望删除以前的cookie。 I am able to unset
the previous cookie but new cookie is not working for me. 我可以
unset
以前的cookie,但是新的cookie对我不起作用。 It showing blank. 它显示为空白。
if(!empty($User_Name))
{
unset($_COOKIE['username']);
setcookie('username', $User_Name, time()+31536000);
echo $_COOKIE['username']; // blank
}
Any help would be nice. 你能帮忙的话,我会很高兴。
In my opinion there is no need to unset the cookie. 我认为没有必要取消设置cookie。 Because, when you set the cookie it will override the existing cookie ( if it exists ) or create a new one ( if it doesn't exist )
因为,当您设置cookie时,它将覆盖现有的cookie(如果存在)或创建一个新的cookie(如果不存在)。
From the PHP Docs .. 来自PHP文档 ..
Cookies will not become visible until the next loading of a page that the cookie should be visible for.
直到下一次加载该cookie的页面加载时,cookie才可见。 To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires.
要测试cookie是否成功设置,请在cookie过期之前在下一个加载页面上检查cookie。
Found that if the path is empty it applies only to the current path, if you specify "/" applies to all paths. 发现如果路径为空,则仅适用于当前路径,如果指定“ /”,则适用于所有路径。
so /
did the trick. 所以
/
做到了。
setcookie('username', $User_Name, time() + (86400 * 7), "/");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.