Deleting Cookies


Want to delete a cookie? Just set the cookie's value to "" and call setcookie with the same parameters as the cookie was set with. When you set the value argument to an empty string (""), and all other arguments match a previous call to setcookie, then the cookie with the specified name will be deleted from the user's machine:

 <?php     setcookie ("message", ""); ?> 

When deleting a cookie, it's also best to make sure you set the expiration date in the past in order to trigger the cookie removal mechanism in the browser:

 <?php     setcookie ("message", "", time() - 3600); ?> 

Here's an example. In this case, we'll delete the message cookie like this:

 <?php     setcookie("message", "", time() - 3600); ?> 

And we'll also direct the user to phpgetcookie.php to confirm that the cookie is gone:

 <?php     setcookie("message", "", time() - 3600); ?> <HTML>     <HEAD>         <TITLE>             Deleting a cookie         </TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>Deleting a cookie</H1>             Cookie was deleted. Look at                 <A HREF="phpgetcookie.php">phpgetcookie.php</A>.          </CENTER>     <BODY> </HTML> 

When you reload this page, the phpgetcookie.php script won't be able to find the cookie any longer.



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net