check if cookies are enabled

check if cookies are enabled  using -'php,javascript,session-cookies'

I am working on a page that requires javascript and sessions. I already have code to warn the user if javascript is disabled. Now, I want to handle the case where cookies are disabled, as the session id is stored in cookies.

I have thought of just a couple ideas:

Embedding the session id in the links and forms
Warn the user they must enable cookies if they are disabled (would need help detecting if cookies are disabled)

What is the best way to approach this? Thanks


Based on the articles linked, I came up with my own approach and thought I would share, somebody else might be able to use it, maybe I will get a few critiques. (Assumes your PHP session stores in a cookie named PHPSESSID)

<div id="form" style="display:none">Content goes here</div>
<noscript>Sorry, but Javascript is required</noscript>
<script type="text/javascript"><!--
   document.write('<p>Sorry, but cookies must be enabled</p>');


asked Sep 21, 2015 by rajnipancholi
0 votes

7 Answers

0 votes


In JavaScript you simple test for the cookieEnabled property, which is supported in all major browsers. If you deal with an older browser, you can set a cookie and check if it exists. (borrowed from Modernizer):

if (navigator.cookieEnabled) return true;

// set and read cookie
document.cookie = "cookietest=1";
var ret = document.cookie.indexOf("cookietest=") != -1;

// delete cookie
document.cookie = "cookietest=1; expires=Thu, 01-Jan-1970 00:00:01 GMT";

return ret;


In PHP it is rather "complicated" since you have to refresh the page or redirect to another script. Here I will use two scripts:



answered Sep 21, 2015 by sameer rathore
0 votes

But to check whether cookies are enabled using isset($_COOKIE["cookie"]) you have to refresh. Im doing it ths way (with sessions based on cookies :)

$a = session_id();

$b = session_id();

if ($a == $b)
    echo"Cookies ON";
    echo"Cookies OFF";
answered Sep 21, 2015 by abhi
0 votes

You cannot in the same page's loading set and check if cookies is set you must perform reload page:

  • PHP run at Server;
  • cookies at client.
  • cookies sent to server only during loading of a page.
  • Just created cookies have not been sent to server yet and will be sent only at next load of the page.
answered Sep 21, 2015 by mca.agarwal
0 votes

A transparent, clean and simple approach, checking cookies availability with PHP and taking advantage of AJAX transparent redirection, hence not triggering a page reload. It doesn't require sessions either.

Client-side code (JavaScript)

function showCookiesMessage(cookiesEnabled) {
    if (cookiesEnabled == 'true')
        alert('Cookies enabled');
        alert('Cookies disabled');

$(document).ready(function() {
    var jqxhr = $.get('/cookiesEnabled.php');

(JQuery AJAX call can be replaced with pure JavaScript AJAX call)

Server-side code (PHP)

if (isset($_COOKIE['cookieCheck'])) {
    echo 'true';
} else {
    if (isset($_GET['reload'])) {
        echo 'false';
    } else {
        setcookie('cookieCheck', '1', time() + 60);
        header('Location: ' . $_SERVER['PHP_SELF'] . '?reload');

First time the script is called, the cookie is set and the script tells the browser to redirect to itself. The browser does it transparently. No page reload takes place because it's done within an AJAX call scope.

The second time, when called by redirection, if the cookie is received, the script responds an HTTP 200 (with string "true"), hence the showCookiesMessage function is called.

If the script is called for the second time (identified by the "reload" parameter) and the cookie is not received, it responds an HTTP 200 with string "false" -and the showCookiesMessage function gets called.

answered Sep 21, 2015 by nimisha.jagtap
0 votes


You could create a cookie using JavaScript and check if it exists:

//Set a Cookie`

//Check if cookie exists`
cookiesEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false`

Or you could use a jQuery Cookie plugin

//Set a Cookie`
$.cookie("testcookie", "testvalue")

//Check if cookie exists`
cookiesEnabled=( $.cookie("testcookie") ) ? true : false`


setcookie("testcookie", "testvalue");

if( isset( $_COOKIE['testcookie'] ) ) {


Not sure if the Php will work as I'm unable to test it.

answered Sep 21, 2015 by mtabakade
0 votes

it is easy to detect whether the cookies is enabled:

  1. set a cookie.
  2. get the cookie

if you can get the cookie you set, the cookie is enabled, otherwise not.

BTW: it is a bad idea to Embedding the session id in the links and forms, it is bad for SEO. In my opinion, it is not very common that people dont want to enable cookies.

answered Sep 21, 2015 by dahiyabecomp
0 votes

First, try to create a test cookie with the setcookie() function, then count the $_COOKIE array variable:

 0) {
    echo "Cookies are enabled.";
} else {
    echo "Cookies are disabled.";

answered Sep 21, 2015 by patilkiran.101