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>";
?>
Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (14 votes, average: 3.00 out of 5)
Loading ... Loading ...
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 12 comments

  1. Bryansu

    on November 11, 2006 at 6:28 pm

    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.

  2. KJ

    on April 12, 2007 at 5:32 pm

    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.

  3. SA

    on January 3, 2008 at 1:16 am

    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.

  4. SA

    on January 3, 2008 at 1:30 am

    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”);
    }

  5. Graham

    on January 19, 2008 at 7:17 pm

    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.

  6. Paul

    on February 8, 2008 at 9:23 pm

    Yes, it must be $_COOKIE['test']

  7. gizzer

    on December 2, 2008 at 12:02 pm

    all this cookie talk has made me hungry

  8. brian

    on January 14, 2011 at 9:39 am

    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.

  9. brian

    on January 14, 2011 at 9:43 am

    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.

  10. brian

    on January 14, 2011 at 11:27 am

    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'].”";
    }
    ?>

  11. Mike

    on March 5, 2011 at 3:28 pm

    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?

  12. Julio

    on July 4, 2011 at 4:19 pm

    What’s wrong with phpinfo()?