找到你要的答案

Q:delete cookie in php

Q:删除cookie的PHP

I am trying to delete a cookie.

I am using setcookie("PHPSESSID", "", time() - 6400); which deletes the cookie just fine.

However it is not entirely deleted. When looking at firebug, under "Response Headers" the cookie is being deleted. However under "Request Headers" the cookie is not deleted (and this affects the code behavior).

Ho do I delete (or modify, or access) this other cookie as well?

Thanks!

我试图删除一个cookie。

我用setcookie(“PHPSESSID”,“”,time() - 6400);删除cookie就好了。

但是它并没有完全删除。当看到萤火虫,在“响应头“cookie被删除。然而,在“请求头”cookie不删除(这会影响代码的行为)。

我删除(或修改,或访问)其他cookie呢?

谢谢!

answer1: 回答1:

I had such problem for my logout code, after hard work and researches I myself finally figured it out and used javascript to solve the problem.

You can easily do that in client-side using script below, you might need to change value of path and host:

document.cookie = "PHPSESSID=; expires=Thu, 01 Jan 1970 00:00:00   UTC;path=/;host=localhost";

我有这样的问题,我的退出代码,经过艰苦的工作和研究,我终于想通了,用JavaScript来解决问题。

You can easily do that in client-side using script below, you might need to change value of path and host:

document.cookie = "PHPSESSID=; expires=Thu, 01 Jan 1970 00:00:00   UTC;path=/;host=localhost";
answer2: 回答2:

You might want to unset the $_COOKIE variable too, by adding a

unset($_COOKIE['PHPSESSID']);

in the next line. That however just affects the currently loaded page.

你可能想将_cookie美元变量也加入

unset($_COOKIE['PHPSESSID']);

在下一行。然而,这只是影响当前加载页面。

answer3: 回答3:

using setcookie("PHPSESSID", "", time() - 6400); expires the cookie like 2 hours ago, try using setcookie("PHPSESSID", "", 1); to expire it at epoch January 1st, 1970.

if that doesn't work you can try adding in the path like this setcookie("PHPSESSID","",time()-6400,"/");

You can try this example from http://www.php.net/manual/en/function.setcookie.php#73484 to remove all cookies, but I'm since this seems to be some sort of supercookie who knows..

// unset cookies
if (isset($_SERVER['HTTP_COOKIE'])) {
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    foreach($cookies as $cookie) {
        $parts = explode('=', $cookie);
        $name = trim($parts[0]);
        setcookie($name, '', time()-1000);
        setcookie($name, '', time()-1000, '/');
    }
}

using setcookie("PHPSESSID", "", time() - 6400); expires the cookie like 2 hours ago, try using setcookie("PHPSESSID", "", 1); to expire it at epoch January 1st, 1970.

如果不工作,你可以尝试添加在这样的小路setcookie(“PHPSESSID”,“”,time() - 6400“/”);

你可以试着从HTTP这个例子:/ / www.php。净/手动/ EN /功能。setcookie。PHP # 73484去除所有的饼干,但我因为这似乎是某种supercookie谁知道..

// unset cookies
if (isset($_SERVER['HTTP_COOKIE'])) {
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    foreach($cookies as $cookie) {
        $parts = explode('=', $cookie);
        $name = trim($parts[0]);
        setcookie($name, '', time()-1000);
        setcookie($name, '', time()-1000, '/');
    }
}
answer4: 回答4:

This code can solve this problem:

session_start(); // initialize session
session_destroy(); // destroy session
setcookie("PHPSESSID","",time()-3600,"/"); // delete session cookie

这个代码可以解决这个问题:

session_start(); // initialize session
session_destroy(); // destroy session
setcookie("PHPSESSID","",time()-3600,"/"); // delete session cookie
answer5: 回答5:

See Example 1 here to delete and destroy a session:

http://php.net/manual/en/function.session-destroy.php

first unset the cookie, then destroy the session.

参见示例1删除和销毁会话:

http://php.net/manual/en/function.session-destroy.php

首先设置cookie,然后销毁会话。

php  cookies  session-cookies