如何在 PHP 中取消设置 cookie?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10526967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to unset cookie in PHP?
提问by arboles
I need to figure out how to unset this cookie. Everything I tried so far has failed.
我需要弄清楚如何取消设置这个cookie。到目前为止我尝试过的一切都失败了。
This is how I am currently unsetting it and it doesn't seem to work.
这就是我目前取消设置它的方式,它似乎不起作用。
setcookie("user_id", $user_id, time() - 7200);
This is how I set it:
我是这样设置的:
setcookie("user_id", $user_id, time() + 7200);
I have this function called set_session_from_cookie()that checks if a cookie is set, and if it is set, it starts a new session using the cookie.
我调用set_session_from_cookie()了这个函数来检查是否设置了 cookie,如果设置了,它会使用 cookie 启动一个新会话。
The problem is that when I use this on my page I am unable to logout. I assume this is because I am unable to unset the session.
问题是当我在我的页面上使用它时,我无法注销。我认为这是因为我无法取消设置会话。
The reason I have this function is if a user wants to be remembered after they end the session, they can restart the session by calling the cookie.
我有这个功能的原因是如果用户在结束会话后想要被记住,他们可以通过调用 cookie 来重新启动会话。
function set_session_from_cookie()
{
if (isset($_SESSION['user_id'])) {
echo '';
} else {
$_SESSION['user_id']=$_COOKIE['user_id'];
}
}
Logout:
登出:
<?php
require'core.php';
session_destroy();
setcookie("user_id", "", time() - 7200);
header('Location:/social_learning/site_pages/starter-template.php');
I set my cookie with the following code:
我使用以下代码设置我的 cookie:
if ($rememberme == "on") {
$user_id = mysql_result($query_run, 0, 'id');
setcookie("user_id", $user_id, time() + 7200);
$_SESSION['user_id'] = $user_id;
redirect('home_page.php');
} else {
if ($rememberme == "") {
echo 'ok';
$user_id = mysql_result($query_run, 0, 'id');
echo $user_id;
$_SESSION['user_id'] = $user_id;
redirect('home_page.php');
}
}
How can I restart the session using the saved cookie without using the function I created? Since the function seems to be causing the user to no longer be able to logout.
如何在不使用我创建的函数的情况下使用保存的 cookie 重新启动会话?由于该功能似乎导致用户无法再注销。
采纳答案by arboles
The solution to this problem was that the I needed to set the correct path to unset the cookie since I was unsetting it from a different file that I originally set it in.
这个问题的解决方案是,我需要设置正确的路径来取消设置 cookie,因为我是从最初设置它的不同文件中取消设置的。
I found out which path I needed to use for the unset by looking for the cookie inside my browser cookies, and once I found the cookie inside my browser, the path was listed near the cookie. So I then set the path to the cookie like so:
我通过在浏览器 cookie 中查找 cookie 来找出我需要使用哪个路径来取消设置,一旦我在浏览器中找到了 cookie,该路径就会列在 cookie 附近。所以我然后像这样设置cookie的路径:
setcookie("user_id", $user_id, time() - 1, "/social_learning/site_pages");
The last parameter is the path. And it worked.
最后一个参数是路径。它奏效了。
My original setcookielooks like this:
我原来的setcookie样子是这样的:
setcookie("user_id", $user_id, time() + 7200, "");
回答by FThompson
Set the cookie's expiration date to a time in the past (like one second after epoch, for example).
将 cookie 的到期日期设置为过去的某个时间(例如epoch后一秒)。
setcookie("yourCookie", "yourValue", 1);
setcookie("yourCookie", "yourValue", 1);
This will cause the cookie to expire.
这将导致 cookie 过期。
1is used instead of 0, because 0sets the cookie to expire at the end of the session.
1用于代替0,因为0将 cookie 设置为在会话结束时过期。
回答by mohamed elbou
There are few security concerns regarding you code, however to answer your question, to unset a cookie in php, all you need to do is to set expiration time to a time in the past:
关于您的代码几乎没有安全问题,但是要回答您的问题,要在 php 中取消设置 cookie,您需要做的就是将过期时间设置为过去的时间:
setcookie("user_id", "", time()-10, "/");
"loginform.php" is not a valid domain, that might be the problem here.
“loginform.php”不是一个有效的域,这可能是这里的问题。
回答by Magento Guy
Look at the php manual for information on setcookie
setcookie的信息看php手册
http://php.net/manual/en/function.setcookie.php
http://php.net/manual/en/function.setcookie.php
These notes should explain the process:
这些注释应该解释这个过程:
bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )
Cookies must be deleted with the same parameters as they were set with. If the value argument is an empty string, or FALSE, and all other arguments match a previous call to setcookie, then the cookie with the specified name will be deleted from the remote client. This is internally achieved by setting value to 'deleted' and expiration time to one year in past.
Because setting a cookie with a value of FALSE will try to delete the cookie, you should not use boolean values. Instead, use 0 for FALSE and 1 for TRUE.
必须使用与设置时相同的参数删除 Cookie。如果 value 参数是空字符串或 FALSE,并且所有其他参数都与先前对 setcookie 的调用匹配,则将从远程客户端删除具有指定名称的 cookie。这是通过将值设置为“已删除”并将过期时间设置为过去一年来在内部实现的。
因为设置值为 FALSE 的 cookie 会尝试删除 cookie,所以不应使用布尔值。相反,使用 0 表示 FALSE,使用 1 表示 TRUE。
回答by Vaibhav Gautam
use this code
使用此代码
setcookie("CookieName", "", time()-(60*60*24), "/");
works everytime for me in every website
在每个网站上每次都对我有用
回答by Yinian Chin
In php manual, you can delete a cookie by setting a expiration date is in the past:
在php手册中,您可以通过设置过期日期过去来删除cookie:
setcookie("key","",time()-3600);
In some case, you should provide path and domain for arguments.
在某些情况下,您应该为参数提供路径和域。
In fact, if you assign a cookie with a empty string, it'll also be unset:
事实上,如果您为 cookie 分配一个空字符串,它也会被取消设置:
setcookie("key","");

