Link to home
Start Free TrialLog in
Avatar of Large_Farva
Large_Farva

asked on

Redirect once (per session) from body tag

There is one page I need redirected but only on first load in a session.  The header file is site-wide so I feel like I am limited to the body tag for this.  

Thanks!
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

At the start of your page, you could check a session variable indicating whether the page has already been redirected. If it's set then don't do the redirect. The following code needs to go before any other output.
<?php
session_start();
if (!isset($_SESSION['redirected'])) {
	$_SESSION['redirected'] = true;
	header('Location: http://www.yourdomain.com/newPage.php');
}
?>

Open in new window

Avatar of Large_Farva
Large_Farva

ASKER

Any issues with this being in the global header file within template...this is a cms?  
As you can tell, I am new to sessions...it looks like the "if" statement there is saying "if this page has been redirected, then send them here".  

should
$_SESSION['redirected'] = true;

Open in new window

be set to "false" if I don't want it to redirect after the first time?
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
$_SESSION['redirected'] = true; is just setting the variable with a value. The line preceding it basically says 'if the redirected variable isn't already set, then set it and redirect.

That line could just as well say $_SESSION['redirected'] = "Yes Siree Bob!";

You don't need to check the value - just that it has been set to something
Thanks!