Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for cookies with PHP (not JS) to use for splash page redirect

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.

like image 662
Karl Avatar asked Feb 18 '10 16:02

Karl


3 Answers

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

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

like image 55
Matteo Riva Avatar answered Oct 07 '22 09:10

Matteo Riva


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.

like image 40
Tomas Markauskas Avatar answered Oct 07 '22 09:10

Tomas Markauskas


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.

like image 1
Karl Avatar answered Oct 07 '22 08:10

Karl