Check cookies with php
Today, an interesting script to check cookies with php. I had to used it because a bad configuration on the server turned off cookie settings, and I have on my server various shopping cart using cookies.
You may get different results according to your register_globals settings.
Remember to reload the script to have the correct results as cookie variables become available on the next page you have declared them.
1 2 3 4 5 6 | <?php setcookie ('test', 'test', time() + 60); echo "value of \$test: $test<br>"; echo "value of \$_COOKIE[test]: ".$_COOKIE[test]."<br>"; echo "value of \$HTTP_COOKIE_VARS[test]: ".$HTTP_COOKIE_VARS[test]."<br>"; ?> |


























This post has 12 comments
Bryansu
You cant use php to do a cookies enable checking. In the initial stage before logging in the member page, you can do a redirect on the script to check if the client browser is cookie enable, but what if the user turn if the cookie half way browsing through your website?
The best way is to use javascript to check, because it’s always client-side script and you don’t need to reload the php page.
KJ
I strongly disagree with the Javascript solution. Lots of users turn off Javascript, so you may not get a reliable answer, whereas a server-side solution is sure to be executed.
SA
The correct solution would be the first check to make sure the user has Javascript enabled and then check to make sure that cookies are enabled. I would agree with Bryansu that using a client side script rather then a server side script would make more sense and be more reliable.
SA
Also i just wanted to say a really easy way to check cookies is with javascripts DOM
if (!navigator.cookieEnabled) {
document.write(“No Cookies”);
}else{
document.write(“Cookies Ok”);
}
Graham
Don’t you have to say:
$_COOKIE['test']
instead of:
$_COOKIE[test]
I use a lot of PHP and that’s always how I’ve done it.
Paul
Yes, it must be $_COOKIE['test']
gizzer
all this cookie talk has made me hungry
brian
doesnt work, because if you have javascript disabled, then it can never refresh the page, which is what needs to happen in order for the cookie to be read. the cookie can only be read after its set, and needs a page refresh before its available. So if the javascript is disabled the cookie might be set, but its not available for the script to test this on one page load.
in order to see what im talking about,
add this at the end of your script, which will kill the cookie each time at teh end of the page load for proper testing,
then disable javascript, leaving cookies enabled, and load the page, you will see only the javascript error, now enable the javascript and you will see the cookie error, each time, until you remove that piece of code.
to rest for cookies, you will need some kind of header() redirect to load a 2nd page to test for the cookie that was enabled on the first page WHEN javascript is enabled.
in essence, this script is only valid for cookies when javascript is ENABLED, therefore it is not a valid test for cookies at all, and it mainly useless.
brian
oops, wrong script post, i was referring to this javascript test
http://snippets.bluejon.co.uk/check4-js-and-cookies/check4-js-and-cookie-enabled-v3-phpcode.php
and add the code
setcookie(“exits”, “”);
to teh end of teh script.
no matter what, you need a header() refresh to another page to test if that cookie was active, theres no way to do it in one swipe.
brian
ok here it is. this code doesnt work on initial page load
<?php
setcookie ('test', "yes", time() + 60);
echo 'value of $_COOKIE[test]: '.$_COOKIE['test']."”;
echo ‘value of $HTTP_COOKIE_VARS[test]: ‘.$HTTP_COOKIE_VARS['test'].”";
setcookie (‘test’, “”, time() + 60);
?>
run that code, and you will get no output, then, comment out the last line;
//setcookie (‘test’, “”, time() + 60);
and it takes the first page refresh to load the cookie, then the 2nd page refresh to display the output.
OK, i hope you guys like this, i got something working here;
<?php
//php cookie check script to warn browser if no cookies are loaded
if(!(isset($_COOKIE['CHECK']))){
if (!(isset($_GET['COOKIE_TEST'])))
setcookie ('CHECK', "cookies are enabled", time() + 60*60);//do a cookie check once per 1 hour
//If no error message yet, then do the redirect to load the cookie, otherwise load the error message
if(!(isset($_GET['COOKIE_TEST']))){
$query = strstr($_SERVER['REQUEST_URI'],'?');
if(!(empty($query)))
$newquery = '&';
else
$newquery = '?' ;
$string = 'COOKIE_TEST';
header('Location: ' . $_SERVER['REQUEST_URI'] . $newquery . $string);
exit;
//setcookie ('test', "", time() + 60);
} else
echo 'You must enable cookies on your browser to use the shopping cart!‘;
} else {
//echo’cookies are enabled’;
//echo $_COOKIE['CHECK'].”";
//echo $HTTP_COOKIE_VARS['CHECK'].”";
}
?>
Mike
Brian, check this one, it works on first page load and subsequent ones.
http://www.pelaphptutorials.com/article/php-checking-to-see-if-a-users-browser-accepts-cookies.html
Also I’m wondering if you need to throw an exit(); after the header redirect?
Julio
What’s wrong with phpinfo()?