views:

2600

answers:

5

I am looking for some code that will return me values if the user has JavaScript enabled or disabled, as well as cookies.

I know this is probably easy to do, but my time constraints are so tight, it hurts. There has to be something out there using php that does this. Ideally I would love to find code that has a page setup with all the possible values that could affect my scripts.

EDIT: Obviously JavaScript may be disabled, but, I am hoping that I can find something out there to test the two cases.

My Solution

For anoyone else looking for code to detect if the users has cookie enabled or disabled, here is what I ended up coming up with from the posts below... you can just drop this at teh top of any page and it works...

<?php
// do a cookie test
if (!isset($_SESSION['cookie_check']))
{
    if (!isset($_GET['cc']))
    {
        // drop a cookie in their bag
        setcookie("cookiecheck", "ok", time()+3600);
        header("Location: ".$common->selfURL()."?cc=1");
        exit(0);
    }
    else
    {
        // do we have a problem?
        if (@$_COOKIE['cookiecheck'] != "ok")
        {
            // we have a problem
            header("Location: /site-diag.php");
            exit(0);
        }
        else
        {
            $_SESSION['cookie_check'] = true;
        }
    }
}
?>
+2  A: 

You could use the jQuery cookie plugin to write a cookie and then see if you can read it back again. That would tell you if cookies were enabled in the client's browser or not.

Andrew Hare
If JavaScript was enabled it would. :-)
Nosredna
+1  A: 

Here is one for checking cookies

http://techpatterns.com/downloads/javascript_check_cookies.php

Kevin
+1  A: 

if javascript is disabled then you can't use jquery or prototype.

write a function that writes a cookie, then tries to read it.

and secondly puts out some js code to the screen that makes a ajax call to a basic php script.

you can use a database to set the boolean results of both tests on the visitor table if there is one.

Derek Organ
A: 

First, realize that you can't use JavaScript to check for cookies if JavaScript is turned off. The usual check for cookies being on is to write one and then read it.

Do you care about the case when cookies are on but JavaScript is off? What are you going to do based on the information?

I found this code here for checking for a cookie via PHP. Doesn't rely on JavaScript. Is PHP your server language?

<?php
class cookieCheck
{

    public function check()
    {
     if (setcookie("test", "test", time() + 100))
     {
      //COOKIE IS SET 
      if (isset ($_COOKIE['test']))
      {
          return "Cookies are enabled on your browser";
      }
      else
      {
          return "Cookies are <b>NOT</b> enabled on your browser";
      }
     }

    }
}
?>
Nosredna
cool thanks, yea I am using PHP. I will give this a shot! :D
Mike Curry
Wouldn't you have to do a redirect before reading the cookie you're setting with setcookie()? If my memory is working right, the first time you run this code you will get the 'NOT enabled' response, but if you were to refresh it would show that the cookie is set.
Wally Lawless
This is not how HTTP works.
Chris Kite
+1  A: 

For checking Javascript, either they have it or they don't. If not, you can use <noscript> tags to display a message asking them to turn it on, put a meta redirect inside, etc. That is the extent of your testing ability.

As for cookies, just try setting a cookie then reading it back! Since you're concerned about Javascript's ability to handle cookies, I assume you already have a cookie library that you are using, meaning that you can just use the set function for a test cookie then the get function to read it back. If the test cookie can't be read back, cookies are off.

Matchu