Lucy Weatherford
Lucy Weatherford

Reputation: 5544

delete cookie in 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!

Upvotes: 0

Views: 9785

Answers (5)

Mahdi Younesi
Mahdi Younesi

Reputation: 7509

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

Upvotes: 3

VCLHD
VCLHD

Reputation: 21

This code can solve this problem:

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

Upvotes: 0

JKirchartz
JKirchartz

Reputation: 18042

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

Upvotes: 2

dqhendricks
dqhendricks

Reputation: 19251

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.

Upvotes: -1

Big-Blue
Big-Blue

Reputation: 429

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.

Upvotes: 1

Related Questions