Jump to content

Archived

This topic is now archived and is closed to further replies.

SCook

Page redirects without using header();

Recommended Posts

Hi gang,

I've got a couple of ideas, but maybe there's a simple solutions out there.  My problem is that I have a page that needs to redirect once the php has been exectured.  Now, the problem is that this page is built using require() for the header and footer portions.  So the middle part, the actuall page, executes the code and then has to go back to the home page.  Now, if I use header(), it fails because the header.php which is required, already exists, and therefore throws an exception.

So, is there another redirect function, or am I going to have to get creative?

Share this post


Link to post
Share on other sites
this is probably a dumb solution, but how about making a variable in your required header and/or footer .php and then wrapping an if statement around the require function like so:

if (!$blah) {
  require ('header.php');
}

or am i totally not understanding the situation

Share this post


Link to post
Share on other sites
header wont fail if you have a file called header.php. It'll only fail if you have some form of out being sent to the browser before the use of the header function.

You might want to use ob_start and ob_end_flush to turn on output buffering. Or you can use good old html to redirect. There is not otrher PHP function you can use to redirect the user. header is only option in PHP.

Share this post


Link to post
Share on other sites
If you are getting an error like "Warning: Cannot modify header information - headers already sent by" read wildteen's post

Share this post


Link to post
Share on other sites
I had a similar problem and my solution was to use this

[code]echo" <meta http-equiv='refresh' content='3;url=http://www.yoursite.com/index.php'>";[/code]

You could chnage the content=3 to however long you like in seconds

Share this post


Link to post
Share on other sites
[quote author=AdRock link=topic=105503.msg421499#msg421499 date=1156441668]
I had a similar problem and my solution was to use this

[code]echo" <meta http-equiv='refresh' content='3;url=http://www.yoursite.com/index.php'>";[/code]

You could chnage the content=3 to however long you like in seconds
[/quote]

I have been told that that meta does not work on IE

Share this post


Link to post
Share on other sites
You could also use javascript:

[code]echo '<script type="text/javascript">window.location.href = "somepage.php";</script>';[/code]

Share this post


Link to post
Share on other sites
Would that work if the user has got javascript disabled in their browser?

Share this post


Link to post
Share on other sites
It's better to redesign your script to send the header()'s before any output. Using output buffering (ob_start/ob_end_flush) does work, but it can be load inducing on the server (all output is stored in memory until flush, instead of direct output to browser)

Also, meta tag refresh is more reliable than header('Location: ... '); as surprisingly, more browsers support meta's than do header. (Some browsers also have options to ignore header redirects)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.