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.

PHP:
  1. <?php
  2. setcookie ('test', 'test', time() + 60);
  3. echo "value of \$test: $test<br>";
  4. echo "value of \$_COOKIE[test]: ".$_COOKIE[test]."<br>";
  5. echo "value of \$HTTP_COOKIE_VARS[test]: ".$HTTP_COOKIE_VARS[test]."<br>";
  6. ?>

Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5 out of 5)
Loading ... Loading ...

6 Responses to “Check cookies with php”

  1. Bryansu on November 11th, 2006 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 12th, 2007 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 3rd, 2008 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 3rd, 2008 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 19th, 2008 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 8th, 2008 9:23 pm

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

Leave a Reply