11

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();
    }
}
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
ppp
  • 2,035
  • 9
  • 32
  • 52

4 Answers4

8

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");
}
Community
  • 1
  • 1
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • Thank you for your time. My problem here is say user wants mypage.com/index.php?page=hello. I want to check if cookies are enabled AND show the page the client requested, so after redirecting to the cookie check scrpt, I have to redirect the user back to the page he wanted in the first place. – ppp Feb 25 '12 at 22:19
  • @AnPel I have updated my answer 1 min ago. check it again. I have added how to get back to old url too. – Shiplu Mokaddim Feb 25 '12 at 22:21
  • Much better. Saving the page to the session seems to be the only option here. Just a clarification, because I tried it earlier and got a syntax error, why do you use {} in your location header? What should I read to understand this? Thanx again. – ppp Feb 25 '12 at 22:27
  • @AnPel See [complex quoted string parsing](http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex) – Shiplu Mokaddim Feb 25 '12 at 22:40
  • If cookies are disabled and session is using cookies only (as the header based redirect suggests), this simply does not work. – hakre Dec 20 '12 at 00:41
1

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();
        }
    }
// ------------------------------- //
ppp
  • 2,035
  • 9
  • 32
  • 52
0

There's no need to save the original URL and redirect to it afterwards. You can perform a transparent redirect via AJAX which doesn't trigger a page reload. It's very simple to implement. You can check my post here: https://stackoverflow.com/a/18832817/2784322

Community
  • 1
  • 1
zibilico
  • 91
  • 1
  • 3
0

I think this is easiest solution. Doesn't require separate files and allows you to proceed with script if cookies are enabled:

$cookiesEnabled = true;

if (!isset($_COOKIE['mycookie'])) {
    $cookiesEnabled = false;
    if (!isset($_GET['cookie_test'])) {
        setcookie('mycookie', 1, 0, '/');
        @ob_end_clean();
        $_SESSION['original_url'] = $_SERVER['REQUEST_URI'];
        $uri = $_SERVER['REQUEST_URI'];
        $uri = explode('?', $uri);
        $q = (isset($uri[1]) && $uri[1])?explode('&', $uri[1]):array();
        $q[] = 'cookie_test=1';
        $uri[1] = implode('&', $q);
        $uri = implode('?', $uri);
        header('Location: '.$uri);
        die;        
    }
} else if (isset($_GET['cookie_test'])) {
    @ob_end_clean();
    $uri = $_SESSION['original_url'];
    unset($_SESSION['original_url']);
    header('Location: '.$uri);
    die;    
}

// if (!$cookiesEnabled) ... do what you want if cookies are disabled