This is the script I wrote to learn cookies - it works fine on this pile of junk I'm testing on:
<?php
//You've got to SET a cookie before anything else because it's part of the html header
// See more comments at the bottom
if (isset($_REQUEST["Submit"])) //am I sending myself something to cookie
{
// setcookie("name",$_REQUEST["cookiethis"]);
$request=$_REQUEST["Submit"];
$value = $request=="Delete Cookie"? NULL : $_REQUEST["cookiethis"];
//setting all the values in a cookie to nothing also deletes it. (on my computer)
setcookie("name",$value, time()+300); // 300/60 = 5 minitos
if($value)
echo "Setting Your Cookie to: ".$_REQUEST["cookiethis"]."<br>You have FIVE minutes before it expires<br>";
else
echo "Deleting your Cookie<br>";
}
else if( isset($_COOKIE["name"]) ) // do they already have a cookie?
{
echo "Welcome<b>: ".$_COOKIE['name']."</b><br>";
}
else
{
echo "Just come here, your first time, is it sir?<br><br>";
}
?>
<form name="form1" method="get" action="cookie.php">
<p>
<input name="cookiethis" type="text" id="cookiethis">
Set/Update your cookie with this</p>
<p>
<input type="submit" name="Submit" value="Set Cookie">
<input name="Submit" type="submit" id="Submit" value="Delete Cookie">
</p>
</form>
<a href="cookie.php"> Reload this Page</a>
<?php
/* When I first set up this example I didn't put an expiry date in the cookie and noticed that a new cookie
wasn't appearing in the cookie directory. I then kicked off two other windows with the script, set names
and noticed that setting a cookie in one window and reloading the other didn't change the value.
So each window had an individual cookie that pissed off as soon as I closed it, just like annoying bastard
Genie. I think that's probably what session cookies are.
*/
?>