Detect if cookies are enabled in PHP

I am trying to detect if a user on my page has cookies enabled or not. The following code performs the check, but, I have no idea on how to redirect the user to the page they came from.

The script starts a session and checks if it has already checked for cookies. If not, it redirects the user to a test page, and since I had called session_start() in the first page, I should see the PHPSESSID cookie if the user agent has cookies enabled.

The problem is, ths script might be called from any page of my site, and I will have to redirect them back to their selected page, say index.php?page=news&postid=4.

session_start();
// Check if client accepts cookies //
if (!isset($_SESSION['cookies_ok'])) {
    if (isset($_GET['cookie_test'])) {
        if (!isset($_COOKIE['PHPSESSID'])) {
            die('Cookies are disabled');
        } else {
            $_SESSION['cookies_ok'] = true;
            header(-------- - ? ? ? ? ? -------- -);
            exit();
        }
    }
    if (!isset($_COOKIE['PHPSESSID'])) {
        header('Location: index.php?cookie_test=1');
        exit();
    }
}

Solution 1:

I think its better to make one file set cookie and redirect to another file. Then the next file can check the value and determine if cookie is enabled. See the example.

Create two files, cookiechecker.php and stat.php

// cookiechecker.php
// save the referrer in session. if cookie works we can get back to it later.
session_start();
$_SESSION['page'] = $_SERVER['HTTP_REFERER'];
// setting cookie to test
setcookie('foo', 'bar', time()+3600);
header("location: stat.php");

and

stat.php

<?php if(isset($_COOKIE['foo']) && $_COOKIE['foo']=='bar'): 
// cookie is working
session_start();
// get back to our old page
header("location: {$_SESSION['page']}");
else:            // show the message ?>
cookie is not working
<? endif; ?>

Load cookiechecker.php in browser it'll tell cookie is working. Call it with command line like curl. It'll say, cookie is not working


Update

Here is a single file solution.

session_start();

if (isset($_GET['check']) && $_GET['check'] == true) {
    if (isset($_COOKIE['foo']) && $_COOKIE['foo'] == 'bar') {
        // cookie is working
        // get back to our old page
        header("location: {$_SESSION['page']}");
    } else {
        // show the message "cookie is not working"
    }
} else {
    // save the referrer in session. if cookie works we can get back to it later.
    $_SESSION['page'] = $_SERVER['HTTP_REFERER'];
   // set a cookie to test
    setcookie('foo', 'bar', time() + 3600);
    // redirecting to the same page to check 
    header("location: {$_SERVER['PHP_SELF']}?check=true");
}

Solution 2:

HTTP_REFERER did not work for me, seems like REQUEST_URI is what I need.

Here is the code I finally used:

session_start();
// ------------------------------- //
// Check if client accepts cookies //
// ------------------------------- //

if( !isset( $_SESSION['cookies_ok'] ) ) {
    if( isset( $_GET['cookie_test'] ) ) {
        if( !isset( $_COOKIE['PHPSESSID'] ) ) {
            die('Cookies are disabled');
            }
        else {
            $_SESSION['cookies_ok'] = true;
            $go_to = $_SESSION['cookie_test_caller'];
            unset( $_SESSION['cookie_test_caller'] );
            header("Location: $go_to");
            exit();
            }
        }
    if( !isset( $_COOKIE['PHPSESSID'] ) ){
        $_SESSION['cookie_test_caller'] = $_SERVER['REQUEST_URI'];
        header('Location: index.php?cookie_test=1');
        exit();
        }
    }
// ------------------------------- //