Hi there,
I recently added the cookie function into a system.
I was thinking if there is anyone out there who know how to use php to detect the user’s browser if the cookie has been disabled?
Thanks….
You will need to set cookie first for example:
setcookie(“TestCookieName”, TRUE);
Then redirect the page, perhaps to itself:
header(“Location: “.$_SERVER['PHP_SELF']);
exit;
Then test for the cookie:
if($_COOKIE['TestCookieName']){
//do something
}else{
//cookie not set, doe something else
}
You must do the redirection as cookies are only read on page load, so you can’t just set the cookie and then test for it. Also setcookie does not return false if the browser refuses the cookie.
Store something into a cookie, read from it. If you successfully read the value you’re expecting, you know cookies are enabled.
You will need to set cookie first for example:
setcookie(“TestCookieName”, TRUE);
Then redirect the page, perhaps to itself:
header(“Location: “.$_SERVER['PHP_SELF']);
exit;
Then test for the cookie:
if($_COOKIE['TestCookieName']){
//do something
}else{
//cookie not set, doe something else
}
You must do the redirection as cookies are only read on page load, so you can’t just set the cookie and then test for it. Also setcookie does not return false if the browser refuses the cookie.
Use the isset() function to see if it was successfully placed. If not, you can alert the user or do whatever you want.