delete cookie in php

余生颓废 提交于 2019-12-20 07:04:50

问题


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!


回答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";



回答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.




回答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, '/');
    }
}



回答4:


This code can solve this problem:

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



回答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.



来源:https://stackoverflow.com/questions/9401654/delete-cookie-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!