Check if cookies are enabled
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!'; }
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!
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";