kamalo
kamalo

Reputation: 186

PHP Redirect with Cookies

How do I redirect to a splash page once with cookies?

I'm setting the cookie on my splash.php page to this:

<?php 
$expire = time()+60;
setcookie("no_splash", "1", $expire);
?>

On that page there's a link to my index.php with this:

<?php
if($_COOKIE['no_splash']== '1') {
    header("Location: index.php");
    echo "it works";
} else if($_COOKIE['no_splash']!= '1') {
    header("Location: splash.php");
};
?>

I keep getting a redirect loop error but can't figure why.

Upvotes: 1

Views: 3553

Answers (3)

Joseph Duffy
Joseph Duffy

Reputation: 4826

Have you tried simply:

<?php
if($_COOKIE['no_splash']== '1') {
    echo "it works";
} else {
    header("Location: splash.php");
};
?>

Maybe isset($_COOKIE['no_splash']) rather than `$_COOKIE['no_splash']== '1'?
Also, not sure it this is what you want, but you can set simply not set the expiration time (or set it to 0), and it will delete the cookie when the browser is closed, so if they keep it open, they won't have to go back to the splash.

Upvotes: 0

Grim...
Grim...

Reputation: 16953

You are redirecting to index.php from the index.php file, hence the loop.

Change your code to be simply

if($_COOKIE['no_splash'] != '1') {
    header("Location: splash.php");
    exit;
}

or indeed

if(!$_COOKIE['no_splash']) {
    header("Location: splash.php");
    exit;
}

which is the same thing.

Upvotes: 2

user956584
user956584

Reputation: 5559

$expire = time()+60;

header("Set-Cookie: no_splash=1; expires=$expire; path=/");
header("Location: index.php");

Upvotes: 0

Related Questions