Cookie check to see if cookies are enabled

One difficult aspect of cookies is determining if they are blocked or not when loading a single page. (for example trying to determine whether someone is the first time visitor to a page or repeat visitor with cookies disabled.)

Here is a nice php technique for testing whether cookies are enabled or disabled


//check for cookies being writeable and persisting.
if (isset($_GET['cookcheck']) && $_GET['cookcheck'] == true) {
//means this is the second load of the page via script
if (isset($_COOKIE['cookieAble']) && $_COOKIE['cookieAble'] == 'true') {
// cookie is working
$bCookieAble = TRUE;
    } else {
//cookies are blocked;
$bCookieAble = FALSE;
} elseif (!isset(
$_GET['cookcheck']) && !isset($_COOKIE['cookieAble'])){
//The cookie ability has not been evaluated yet, so start the test
    // due to the impending page re-load, save the referrer in session. if cookie works we can get back to it if we need it
$_SESSION['pageReferer'] = $_SERVER['HTTP_REFERER'];
// set a cookie to test
setcookie('cookieAble', 'true', time() + 3600);
// redirecting to the same page with "cookcheck" query added in order to reload the script to check to see if the cookie exists
header("location: {$_SERVER['PHP_SELF']}?{$_SERVER['QUERY_STRING']}&cookcheck=true");

//The rest of your page script will follow here but will now have access to the $bCookieAble to be able to use in your code below.  consider using either an if else if or a switch statement