当我想要删除Cookie时,我会尝试
unset($_COOKIE['hello']);
我在firefox的cookie浏览器中看到cookie仍然存在。我怎么才能真正去掉饼干呢?
当我想要删除Cookie时,我会尝试
unset($_COOKIE['hello']);
我在firefox的cookie浏览器中看到cookie仍然存在。我怎么才能真正去掉饼干呢?
当前回答
只要将过期日期设置为一小时前,如果你想“删除”cookie,就像这样:
setcookie ("TestCookie", "", time() - 3600);
or
setcookie ("TestCookie", "", time() - 3600, "/~rasmus/", "example.com", 1);
来源:http://www.php.net/manual/en/function.setcookie.php
你应该使用filter_input()函数来处理访问者可以输入/操作的所有全局变量,如下所示:
$visitors_ip = filter_input(INPUT_COOKIE, 'id');
你可以在这里阅读更多:http://www.php.net/manual/en/function.filter-input.php和这里:http://www.w3schools.com/php/func_filter_input.asp
其他回答
这是如何PHP v7 setcookie()代码工作时,你这样做:
<?php
setcookie('user_id','');
setcookie('session','');
?>
从tcpdump在端口80上嗅探时的输出,服务器向客户端(浏览器)发送以下HTTP报头:
Set-Cookie: user_id=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0
Set-Cookie: session=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0
观察以下请求中的数据包,浏览器不再在报头中发送这些cookie
$cookie_name = "my cookie";
$cookie_value = "my value";
$cookie_new_value = "my new value";
// Create a cookie,
setcookie($cookie_name, $cookie_value , time() + (86400 * 30), "/"); //86400 = 24 hours in seconds
// Get value in a cookie,
$cookie_value = $_COOKIE[$cookie_name];
// Update a cookie,
setcookie($cookie_name, $cookie_new_value , time() + (86400 * 30), "/");
// Delete a cookie,
setcookie($cookie_name, '' , time() - 3600, "/"); // time() - 3600 means, set the cookie expiration date to the past hour.
将值设置为"",过期日期设置为昨天(或过去的任何日期)
setcookie("hello", "", time()-3600);
然后,cookie将在下次页面加载时过期。
我在php中使用这个,它工作得很好。
function cookie_unset()
{
setcookie("cookie_name", "", time() - 3600, '/');
unset ($_COOKIE['cookie_name']);
//done
}
一个干净的删除cookie的方法是清除$_COOKIE值和浏览器cookie文件:
if (isset($_COOKIE['key'])) {
unset($_COOKIE['key']);
setcookie('key', '', time() - 3600, '/'); // empty value and old timestamp
}