I am having some problems with setting a cookie.
Here is what I am trying to achieve with my 'index.php'
script:
If someone arrives at my site, using the value
of 'id' as passed in the query string:
eg. http://www.mydomain.com/index.php?id=fred
....two possible things should happen:
If the cookie is not already set, then set it
If the cookie is already set, then I want the
option of being able to define whether the existing
cookie gets overwritten (ie. $write_over_cookie = 1)
OR to not overwrite and just keep the existing cookie
(ie. $write_over_cookie = 0)
(Actually, the third option is if someone arrives at my
site 'http://www.mydomain.com' with no 'id' passed then
disregard setting a cookie altogether)
However, something is wrong with my code and I don't
know what - when I do a test by typing the following
directly into my browser:
http://www.mydomain.com/index.php?id=fred OR even just:
http://www.mydomain.com
....I don't get any sort of error back - rather my browser
just sits there forever and a day like it's doing something
but nothing actually happens.
Any tips or pointers anyone can give me on this?
regards,
Adam
P.S. Here is my 'index.php' file:
<=== SNIP ===>
<?php
$cookiename = 'KILLER'; // Cookie name
$cookiedays = '365'; // Nbr of Days before cookie will expire
$domain = 'mydomain.com'; // Domain this cookie is applicable for
$write_over_cookie = '1'; // If this cookie already exists overwite it
// with a new cookie if 'id' is passed in query string
// Use '1' for 'yes' and '0' for 'no'
if (!isset($_COOKIE["$cookiename"]) && isset($GET['id']))
{
setcookie($cookiename, $id, time()+606024*$cookiedays, "/", ".$domain");
header("Location: $PHP_SELF");
exit;
}
if (isset($COOKIE["$cookiename"]) && isset($GET['id']) && $write_over_cookie == 1 )
{
setcookie($cookiename, $id, time()+606024*$cookiedays, "/", ".$domain");
header("Location: $PHP_SELF");
exit;
} else {
header("Location: $PHP_SELF");
}
?>
<HTML>
<HEAD>
<TITLE>Cookie Test</TITLE>
</HEAD>
<BODY>
Normal content in here!
</BODY>
</HTML>
<=== /SNIP ===>