Check if cookies are enabled
Solution 1:
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!
Solution 2:
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 :)
session_start();
$a = session_id();
session_destroy();
session_start();
$b = session_id();
session_destroy();
if ($a == $b)
echo"Cookies ON";
else
echo"Cookies OFF";
Solution 3:
Answer on an old question, this new post is posted on April the 4th 2013
To complete the answer of @misza, here a advanced method to check if cookies are enabled without page reloading. The problem with @misza is that it not always work when the php ini setting session.use_cookies
is not true. Also the solution does not check if a session is already started.
I made this function and test it many times with in different situations and does the job very well.
function suGetClientCookiesEnabled() // Test if browser has cookies enabled
{
// Avoid overhead, if already tested, return it
if( defined( 'SU_CLIENT_COOKIES_ENABLED' ))
{ return SU_CLIENT_COOKIES_ENABLED; }
$bIni = ini_get( 'session.use_cookies' );
ini_set( 'session.use_cookies', 1 );
$a = session_id();
$bWasStarted = ( is_string( $a ) && strlen( $a ));
if( !$bWasStarted )
{
@session_start();
$a = session_id();
}
// Make a copy of current session data
$aSesDat = (isset( $_SESSION ))?$_SESSION:array();
// Now we destroy the session and we lost the data but not the session id
// when cookies are enabled. We restore the data later.
@session_destroy();
// Restart it
@session_start();
// Restore copy
$_SESSION = $aSesDat;
// If no cookies are enabled, the session differs from first session start
$b = session_id();
if( !$bWasStarted )
{ // If not was started, write data to the session container to avoid data loss
@session_write_close();
}
// When no cookies are enabled, $a and $b are not the same
$b = ($a === $b);
define( 'SU_CLIENT_COOKIES_ENABLED', $b );
if( !$bIni )
{ @ini_set( 'session.use_cookies', 0 ); }
//echo $b?'1':'0';
return $b;
}
Usage:
if( suGetClientCookiesEnabled())
{ echo 'Cookies are enabled!'; }
else { echo 'Cookies are NOT enabled!'; }
Important note: The function temporarily modify the ini setting of PHP when it not has the correct setting and restore it when it was not enabled. This is only to test if cookies are enabled. It can get go wrong when you start a session and the php ini setting session.use_cookies has an incorrect value. To be sure that the session is working correctly, check and/or set it before start a session, for example:
if( suGetClientCookiesEnabled())
{
echo 'Cookies are enabled!';
ini_set( 'session.use_cookies', 1 );
echo 'Starting session';
@start_session();
}
else { echo 'Cookies are NOT enabled!'; }
Solution 4:
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');
else
alert('Cookies disabled');
}
$(document).ready(function() {
var jqxhr = $.get('/cookiesEnabled.php');
jqxhr.done(showCookiesMessage);
});
(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');
exit();
}
}
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.