[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

How can I detect if session cookies are disabled?

How can I detect from client side JavaScript that session cookies are disabled?

Persistent cookies can be detected with navigator.cookieEnabled, but session cookies not.

And you can disable them easy in IE up to 6 and not so easy on IE 6 and up. Also easy on Netscape.

A server solution is not acceptable (it includes writing a cookie and redirecting to check it).
1 Solution
I'm not sure i agree with you. If navigator.cookieEnabled returns true, that means the browser is accepting cookies(maybe options for third party etc). There may be an option to accept session cookies but not persistent, but I don't recall any user setting which allows persistent but not session cookies.
ccaprarAuthor Commented:

At least as IE 5 is concerned, you can disable separately both types of cookies.
In IE6, you can do this also, but using the Privacy settings.

MSDN states that navigator.cookieEnabled ignores session cookies....
I'm not sure I understood your question, but
why don;t you try, seting a cookie , then issue
a <META> tag with a refresh argument, and on
next page, try to get the cookie, if it is available
it may be an incicator that cookies are enabled...

if not, you can then for example redirect to another
page using again the <META> tag.

Hope it helps.

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

I think you may be unclear on the timing difference between server set and client-side JS set cookies.  When you set from the server you use HTTP headers, and can't read them on the same page (need that redirect).  But that's not the case with JS set cookies.  You can set the cookie (and set is as a session cookie) and then try to read from it all on the same page.  If you can't read from it after it's set, then their cookies are disabled.

Now of course... that raises the issue of how do you tell if JS is enabled, which is another story.  Unless you really only care about the combo of JS + cookies, in which case on JS cookie test will tell you everything (have the test change a form field to 'enabled' from 'disabled' if the test succeeds to get the info back to server side).

Don't rely on META Refresh.... the META Refresh can also be disabled in the browser completely separately from JS or cookies so your test could fail even if the condition you were testing for was true.
ccaprarAuthor Commented:
Hi memerot.

Asuming that I have JS enabled, how can I detect in JS if a session cookie and a persistent cookie are allowed? Can you give me a small code example?

try to set and then get a simple cookie, for example:

function GetCookie (name)
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen)
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
    return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  return null;

function SetCookie (name, value)
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");

var cookies_enabled = "";
var exp = new Date();
exp.setTime(exp.getTime() + (60*1000));
SetCookie ('testcookie', 1, exp);
cookies_enabled = GetCookie('testcookie');


if (enabled==null)
  // cookies off
  //cookies on

I haven't used this code, because I needn't it.
I've just seen something like this somewhere in the internet.

I hope I told you what you needed :)
ccaprarAuthor Commented:
Thanks, I will check that.
no problems

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now