Tuesday, May 15, 2012

Logging Out a User


This is a short post about how to delete cookies on a user's computer.

Deleting A Cookie

It is trivial to delete a cookie. All you need to do is know the name of the cookie. Then, just set the data portion to an empty string, and the expiry date to some time in the past.

setcookie("user", "", time() - 3600);

Deleting All Cookies

Alternatively, you can opt to delete all the cookies that belong to you. You can achieve this by looping through the $_COOKIE array. Note that this will also delete cookies you didn't directly create, such as Google Analytics cookies.

function clearAllCookies() {
 foreach ($_COOKIE as $name => $value) {
  setcookie($name, "", time() - 3600);

No comments:

Post a Comment