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!
Large_FarvaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
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

0
Large_FarvaAuthor Commented:
Any issues with this being in the global header file within template...this is a cms?  
0
Large_FarvaAuthor Commented:
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?
0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Chris StanyonWebDevCommented:
That depends :)

As long as the global header file is called before any output to the browser then you should have no problems. (the header() function won't work correctly if it's called after any browser output)

Also, if you call it in a global header file as it is, then it will redirect any page to the new one. If you only want it to redirect from a specific page, then you'll need to include some code to check the name of the current page.
<?php
session_start();

$currFilename = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);

//only redirect if the current page is called myPage.php and it hasn't already been redirected.
if ($currFilename == "myPage.php") {
	if (!isset($_SESSION['redirected'])) {
		$_SESSION['redirected'] = true;
		header('Location: http://www.yourdomain.com/newPage.php');
	}
}
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris StanyonWebDevCommented:
$_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
0
Large_FarvaAuthor Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JScript

From novice to tech pro — start learning today.