PHP and Cookies; a good mix!
(Page 3 out of 5)Before you start using cookies, you must make sure your visitor has cookies enabled. This can be done with a simply PHP checking script. Unfortunately, the PHP page needs to reload to check for cookies. But this can be done very transparently, and your visitor should hardly notice anything.
The following example will first set a test cookie, then reload the page, and finally check whether cookies are enabled.
error_reporting (E_ALL ^ E_WARNING ^ E_NOTICE);
// Check if cookie has been set or not
if ($_GET['set'] != 'yes') {
// Set cookie
setcookie ('test', 'test', time() + 60);
// Reload page
header ("Location: checkcookies.php?set=yes");
} else {
// Check if cookie exists
if (!empty($_COOKIE['test'])) {
echo "Cookies are enabled on your browser";
} else {
echo "Cookies are NOT enabled on your browser";
}
}
?>
Run the code above, and see what the output is. Check if cookies are enabled in your browser. If they're not enabled, then you can enable them by going to your browser's options. Unfortunately, this is different from each browser, so I can't give you exact instructions. But Google can.
February 16th, 2006 at 3:09 pm
This site is very helpful to me.
I expect more examples about session and cookies from this site.
Thanks a lot.