Training, Open Source
computer languages


PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Deleting a cookie

Posted by admin (Graham Ellis), 19 February 2005
Q: How do I delete a cookie?

A: Set it with an expiry time in the past

There's an example on our web site at http://www.wellho.net/demo/cookie_demo.php4.

Here's the source code of that page:

Code:
<?php
$year = 3600*24*365;
if (! is_string($_COOKIE[userkey])) {
       $received = "No cookie received - you are a NEW visitor";
       $newcomer = 1;
       $cval = uniqid("rn:");
       setcookie("userkey",$cval,time()+$year);
       $say = "Your cookie called <b>userkey</b>".
               " will be set to $cval";
} else {
       $received = "Cookie received - you are a RETURNING visitor<br>".
               "Cookie value received - $_COOKIE[userkey]";
       $newcomer = 0;
       if ($_GET[logout]) {
               $newcomer = -1;
               setcookie("userkey","gone",time()-$year);
               $say = "Your cookie called <b>userkey</b>".
                       " has been deleted";
       } else {
               $say = "Your cookie called <b>userkey</b>".
                       " will NOT be altered";
       }
}

?>
<head>
<title>Setting and clearing a persistant cookie</title>
</head>
<body bgcolor="white">
<h2>Setting, reading and deleting a cookie</h2>
When you visit this page for the first time, you'll set a
cookie called <b>userkey</b> to a unique id starting with
"rn:".  The link below to "refresh" the page will visit the
page again, and the link to "logout" will visit the page
again, deleting the cookie.<br><hr><br>
<b>Current status:</b><br><br>
<?= $received ?><br><br><?= $say ?><br><hr><br>
<b>Actions available:</b><br><br>
You may <a href=<?=$PHP_SELF?>>reload this page</a><br>
<?php if ($newcomer != -1) { ?> You may
<a href="<?=$PHP_SELF?>?logout=1">log out</a><br><?php } ?>
You may <a href=/index.html>go to our home page</a><br><hr>
Example written by Graham Ellis of Well House Consultants<br>
We're in the business of training you on subjects like this!<br>
website: <a href=http://www.wellho.net>http://www.wellho.net</a>
</body>
</html>




This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.


WELL HOUSE CONSULTANTS LTD.: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho