Are Javascript / Cookies Enabled or Disabled?

I'm 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 limits are so tight that it hurts. There must be something using php that does this. Ideally, I would like 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 hope I can find something there to test these two cases.

My decision

For an anonymous code search, to determine if cookies are enabled or disabled, this is what I came up with from the posts below ... you can simply discard this to the 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;
        }
    }
}
?>
+3
source share
6 answers

First, understand that you cannot use JavaScript to validate cookies if JavaScript is disabled. The usual check that cookies are enabled is to write one and then read it.

Do you like the case when cookies are enabled but JavaScript is disabled? What are you going to do based on the information?

cookie PHP. JavaScript. PHP ?

<?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";
            }
        }

    }
}
?>
-6

jQuery cookie cookie, , . , cookie .

+2

Javascript , . , <noscript>, , - .. .

cookie, cookie, ! Javascript cookie, , cookie, , , set cookie, get, , cookie , cookie .

+2

javascript , jquery .

, cookie, .

and secondly, it displays some js code that makes an ajax call to the underlying php script.

you can use the database to set the logical results of both tests in the visitor table, if any.

+1
source

So I check if cookies and JavaScript are enabled:

if($_SESSION['JSexe']) { // 3rd check js
    if($_COOKIE['JS']) {
        setcookie('JS','JS',time()-1); // check on every page load
    }
    else {
        header('Location: js.html');
    }
}
// 2nd so far it been server-side scripting. Client-side scripting must be executed once to set second cookie.
// Without JSexe, user with cookies and js enabled would be sent to js.html the first page load.
elseif($_COOKIE['PHP']) {
    $_SESSION['JSexe'] = true;
}
else { //1st check cookies
    if($_GET['cookie']) {
        header('Location: cookies.html');
    }
    else{
        setcookie('PHP','PHP');
        header('Location: '.$_SERVER['REQUEST_URI'].'?cookie=1');
    }
}

Here is explained in detail: http://asdlog.com/Check_if_cookies_and_javascript_are_enable

0
source

Source: https://habr.com/ru/post/1711102/


All Articles