Can I check if cookies are allowed with modernizr?

I studied how to check if cookies were enabled in the browser, and I found many answers, I even checked a few, but after that my friend suggested I use Modernizr .
I started searching about it and I found a lot of things related to CSS3 and HTML5 , but I don’t want this, I just want to know if I can make sure cookies are enabled or not with Modernizr ?

+7
source share
4 answers
+6
source

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); } 
+8
source

The direct answer to the question: "Yes!" and it is built in

Code example:

 if (Modernizr.cookies == false) { alert('Please enable cookies'); } else { // do something with cookies } 

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

 .cookies #noCookies { display: none; } <div id='#noCookies'> This site requires cookies! Please turn them on already! </div> 

(This .cookies class has .cookies added to the <body> from Modernizr).

Note. If you are creating a custom assembly of Modernizr, the cookies option is currently "hidden" in the "Conventional definitions" section.

+4
source

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 ?> 
0
source

All Articles