views:

347

answers:

3

I want to redirect to a splash page for first time visitors to the site using cookies to remember that they came. This is easy, however, for people with cookies disabled, I never want to redirect them to the splash page.

The problem I'm running into is that when I set the cookie, PHP won't see it until I reload the page. This means first time visitors are not redirected unless they visit the home page twice or reload the page.

Here's what I have that works but requires a reload:

setcookie("test",'1',time() + 3600,'/');
if(isset($_COOKIE['test'])){
    if(isset($_COOKIE['bfc_splash'])){}else{
        header("Location: splash/");
    }
}

I tried this, but it seems SESSION info is stored in a cookie, because it just infinitely redirects:

if(isset($_COOKIE['test'])){
    if(isset($_COOKIE['bfc_splash'])){}else{
        header("Location: splash/");
    }
}elseif(!isset($_COOKIE['test']) && !isset($_SESSION['cookies'])){
    setcookie("test",'1',time() + 3600,'/');
    $_SESSION['cookies'] = '1';
    header("Location: index.php");
}

I would prefer not to use javascript to check for cookies, since someone who disables cookies is likely to also disable javascript. Any insight or links to solutions would be much appreciated. I haven't found much so far.

+2  A: 

If the user has cookies disabled, you won't be able to tell whether he's visiting the site for the first time or not, because every visit will look like the first visit.

Tomas Markauskas
Right, thats why I *never* want to redirect someone with cookies disabled to the splash page.
Karl
You won't be able to tell whether the user has cookies disabled or is visiting the site for the first time.
Tomas Markauskas
+1  A: 

Looking at an example site, barackobama.com, they pass a GET variable when you skip to the home page from the splash redirect. This allows someone to view the home page if they don't have cookies enabled. They don't seem to care too much that anyone with cookies disabled will be redirected again once they click the home link at the top (which lacks the GET variable). Since they don't seem to care about people with cookies disabled, I'll follow suit since I can't find a better solution.

Karl
+2  A: 

You can use a session and propagate the session id via GET instead of cookie.

See: http://php.net/manual/en/session.idpassing.php

kemp