Is it possible to check if cookies are enabled with modernizr?

天大地大妈咪最大 提交于 2019-12-12 07:26:35

问题


I was researching about how to check if the cookies are enabled in a browser and i found a lot of answer, i even tested a few ones, but after that a friend of mine suggest me to use Modernizr for that.
I started to search about that and i found a lot of stuff related with CSS3 and HTML5, but i don't want that, i just wanna know if is it possible to check that cookies are enabled or not with Modernizr?


回答1:


check this url, hope it's helpful :

https://github.com/Modernizr/Modernizr/commit/33f00fbbeb12e92bf24711ea386e722cce6f60cc




回答2:


Below code is copied from http://sveinbjorn.org/cookiecheck.

function are_cookies_enabled()
{
    var cookieEnabled = (navigator.cookieEnabled) ? true : false;

    if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled)
    { 
        document.cookie="testcookie";
        cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
    }
    return (cookieEnabled);
}



回答3:


A direct answer to the question is 'Yes!' and it is built in

Example code:

if (Modernizr.cookies == false) {

    alert('Please enable cookies');    
}
else { 
    // do something with cookies
}

You can also use the css class .cookies or .no-cookies to show/hide a panel telling the user they need cookies enabled.

.cookies #noCookies
{
    display: none;
}

<div id='#noCookies'>
   This site requires cookies! Please turn them on already!
</div>

(This .cookies class is added to <body> tag by Modernizr).

Note: If you are creating a custom build of Modernizr the cookies option is currently 'hidden' under the 'Non-core detects' section.




回答4:


Another way with PHP

HTML/PHP:

<?php
  session_start();
  $_SESSION['cook'] = 1;
  echo "<img src=\"cookcheck.php\">";
?>

PHP - cookcheck.php:

 <?php
     session_start();

     if ($_SESSION['cook'] !== 1) 
                             { $image="/nocookmsg.png"; }    # Cookies NOT Enabled
                               else { $image="/blank.png"; } # Cookies Enabled

     $img=imageCreateFromPNG($image);   # Create Image
     header("Content-type: image/png"); # Send Header
     imagePNG($image);                  # Send Image
 ?>


来源:https://stackoverflow.com/questions/9130457/is-it-possible-to-check-if-cookies-are-enabled-with-modernizr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!