Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if cookies are enabled

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:

  1. Embedding the session id in the links and forms
  2. 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

EDIT

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"><!-- if(document.cookie.indexOf('PHPSESSID')!=-1)    document.getElementById('form').style.display=''; else    document.write('<p>Sorry, but cookies must be enabled</p>'); --></script> 
like image 645
steveo225 Avatar asked Jul 12 '11 12:07

steveo225


People also ask

How do I know if my browser cookies are disabled?

To check whether a setting a cookie is enabled in the browser, you can use the cookieEnabled property in the window. navigator global object in JavaScript. The property will return a Boolean true if cookie enabled and return false if not enabled.

How do I enable cookies on Safari browser?

Step 1: Go to Settings, then scroll down and select “Safari”. Step 2: Scroll down to “Privacy & Security”. Step 3: Verify “Block All Cookies” is ticked (green/white), click to allow cookies. Step 4: Clear the browser cache and reopen the browser.

Do I need cookies enabled on my browser?

Cookies are less of a "safety" issue, and more of a "potential for tracking you/privacy" issue. For the most part, cookies are a safe and necessary part of using the Internet. A lot of websites won't work properly if you don't have Cookies enabled.


1 Answers

JavaScript

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; 
  • Modernizer cookie check commit
  • Checking if Cookies are Enabled

PHP

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

somescript.php

<?php session_start(); setcookie('foo', 'bar', time()+3600); header("location: check.php"); 

check.php

<?php echo (isset($_COOKIE['foo']) && $_COOKIE['foo']=='bar') ? 'enabled' : 'disabled'; 
  • Detecting if the cookies are enabled with PHP
  • PHP and Cookies, A Good Mix!
like image 113
Sascha Galley Avatar answered Sep 30 '22 05:09

Sascha Galley