/    Sign up×
Community /Pin to ProfileBookmark

How to remove ‘PHPSESSID’ cookie?

Hi. How do I force removal of a session cookie using Javascript or PHP functions?

My application set this cookie named ‘PHPSESSID’ and expires at end of session. I used session_start() at beginning.

How do I force removal of this cookie without shuttind down browser?
session_destroy does not remove this cookie.
Thanks.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscJan 28.2008 — what do you mean it sets the cookie that expires at the end of the session?

is it a session variable? (i.e. $_SESSION['PHPSESSID']) or a cookie? If it's a session variable then destroying the session should've worked so I'll assume it's a cookie. You can remove it by setting it with a previous time fo it to expire

[code=php]
setcookie('phpsessid','value',time()-1);
[/code]
Copy linkTweet thisAlerts:
@TJ111Jan 28.2008 — From the PHP manual

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.

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.

...


When deleting a cookie you should assure that the expiration date is in the past, to trigger the removal mechanism in your browser. Examples follow how to delete cookies sent in previous example:

Example 2. setcookie() delete example
[code=php]
<?php
// set the expiration date to one hour ago
setcookie ("TestCookie", "", time() - 3600);
setcookie ("TestCookie", "", time() - 3600, "/~rasmus/", ".example.com", 1);
?> [/code]




[/QUOTE]
Copy linkTweet thisAlerts:
@ploceusauthorJan 30.2008 — I think an example suffices here.

There is no setcookie() command to set the cookie. This cookie (named PHPSESSID) is created automatically and remains until browser is closed.

It seems the only way to remove this cookie while browser (Firefox) is active is to go to Edit->Preferences->Privacy->Cookies->View Cookies, select the PHPSESSID cookie and press "Remove Cookie"

How do I remove this cookie within PHP script? Is there a function (or Javascript) that removes it?

Thanks.

[code=php]
<?php
ini_set("session.use_cookies", "on");
ini_set("session.use_trans_sid", "on");
session_start()
?>
[/code]
Copy linkTweet thisAlerts:
@jdforsytheJan 05.2009 — I believe what you're looking for is this (example of a logout script):
[code=php]
session_start(); // initialize session
session_destroy(); // destroy session
setcookie("PHPSESSID","",time()-3600,"/"); // delete session cookie
[/code]

If you add in the "/" as the domain, it should then delete the session cookie. You can verify by running your tests through a proxy, like The Proxomitron (great software! see exactly what exchanges are going on between the browser and your site) or just look at your cookies from within your browser. This method works for me.


I think an example suffices here.

There is no setcookie() command to set the cookie. This cookie (named PHPSESSID) is created automatically and remains until browser is closed.

It seems the only way to remove this cookie while browser (Firefox) is active is to go to Edit->Preferences->Privacy->Cookies->View Cookies, select the PHPSESSID cookie and press "Remove Cookie"

How do I remove this cookie within PHP script? Is there a function (or Javascript) that removes it?

Thanks.

[code=php]
<?php
ini_set("session.use_cookies", "on");
ini_set("session.use_trans_sid", "on");
session_start()
?>
[/code]
[/QUOTE]
Copy linkTweet thisAlerts:
@PrasathDec 21.2018 — @jdforsythe#965046

session_start(); // initialize session

session_destroy(); // destroy session

setcookie("PHPSESSID","",time()-3600,"/");

This code is working fine, during logout. Thank you

During Login,

$session_lifetime = 3600 * 24 * 6; //6days

session_set_cookie_params($session_lifetime);

session_start();


notes:

session_set_cookie_params will set the session lifetime first, after that call the session_start() function.

Because if you call the session_start() on the starting of the program, the cookie PHPSESSID will set there default expire time (1969-12-31T23:59:59.000Z). This time is valid for 24 minutes if the user keeps the page ideal.
Copy linkTweet thisAlerts:
@rootDec 21.2018 — {"locked":true}
×

Success!

Help @ploceus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...