Hello,
I am trying to create a script which first checks if the user has cookies enabled or not. If they are then give the cookie a unique random value. If the page is reloaded then I don't want to give the cookie a new value, but give it back its current value and re-extend its expiry time. During testing, the cookie expires after only 60 seconds.
However, even though I have cookies enabled, sometimes I recieve the message "Cookies disabled", or when I disable cookies sometimes a cookie is created. I am also having problems where sometimes the cookie keeps its current value whilst most of the time on refresh (within say 2 seconds) the cookie is given a new value, when it should retain its previous value.
Firstly am I doing this in the correct way and secondly what am I doing wrong for these strangely varying issues to occur? I am testing in IE 8 and Firefox 3.5.7.
Thank you,
Chris
I am trying to create a script which first checks if the user has cookies enabled or not. If they are then give the cookie a unique random value. If the page is reloaded then I don't want to give the cookie a new value, but give it back its current value and re-extend its expiry time. During testing, the cookie expires after only 60 seconds.
However, even though I have cookies enabled, sometimes I recieve the message "Cookies disabled", or when I disable cookies sometimes a cookie is created. I am also having problems where sometimes the cookie keeps its current value whilst most of the time on refresh (within say 2 seconds) the cookie is given a new value, when it should retain its previous value.
Firstly am I doing this in the correct way and secondly what am I doing wrong for these strangely varying issues to occur? I am testing in IE 8 and Firefox 3.5.7.
Code:
<?php
if (!isset($_GET['c'])) {
set_cookie (uniqid(time(),true));
header ('Location:' . $_SERVER['PHP_SELF'] . '?c=1');
}
if ((isset($_GET['c'])) and ($_GET['c'] == 1)) {
if (!isset($_COOKIE['b-id'])) {
echo "<p>Cookies disabled.</p>";
}
else {
$cookie_val = $_COOKIE['b-id'];
set_cookie ($cookie_val);
echo "<p>b-id = $cookie_val</p>";
}
}
function set_cookie ($cookie_val) {
setcookie ('b-id', $cookie_val, time() + 60);
}
?>
Thank you,
Chris