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 (10 votes, average: 3.40 out of 5)
Loading ... Loading ...
If you found this post useful, please consider a small donation.

7 Responses

  1. Bryansu says:

    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 says:

    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 says:

    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 says:

    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 says:

    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 says:

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

  7. gizzer says:

    all this cookie talk has made me hungry

Leave a Reply

flash games company