Reputation: 142921
The canonical way (as far as I can tell) to redirect to a different page in PHP is this:
header("Location: URL");
exit();
But let's suppose I need to redirect after part of the page has been rendered. I'm used to using ASP.NET where I could just do this anywhere I wanted
Response.Redirect("URL");
but I can't figure how to do something equivalent in PHP. Is it possible? If so, how, and if not, why not?
Upvotes: 2
Views: 7880
Reputation: 21
or, you can use html redirect for redirecting page.
<meta http-equiv="refresh" content="0; url=xxx.php?page=2">
But, page is waiting aproximately 1 second on this line before redirecting page.
(I am sorry friends. My english is not fluency :(( )
I hope this information benefits to your business.
Upvotes: 2
Reputation: 7882
I would suggest storing the url in a variable, and redirecting at the end. During this process you shouldn't send any content either, but instead store that and send it all at the end to ensure you don't get "headers sent" errors.
$url = '/posts';
// code code code
// oops, we want to redirect elsewhere now
$url = '/tags';
// end of script, send *content* and headers
header("Location: $url");
Obviously there's huge room for improvement here, but you get the idea. Don't send headers or content until your request is done.
Upvotes: 1
Reputation: 5397
It is not possible by using the header function as its purpose is to send headers (not only to redirect users) and you can't do this when the output is started as it is too late
One quick fix would be to use javascript for this, but this is not an "elegant" solution, not to mention that it doesn't work if the visitor has javascript disabled or some error above in the javascript code causes problems.
I think the best way to be sure that you can redirect at any point is to make sure that you don't render any output up until the script is completed. To do this you should use at least a template engine to ensure that you separate your php code from html and that the content is sent to the users just at the end.
You could also use output buffering, because this way you can delay the moment when output is sterted (when the output buffer size is exceeded or you flush it yourself; see php.net/ob_start for this; you would need to do something similar to this:http://codepad.org/AomD4Sok )
PS: don't forget about die/exit after the redirect, no mater how you will do it
Upvotes: 3
Reputation: 43168
You could implement your own Response
class with semantics similar to what you are used to, and use an instance of it to accumulate the output to be sent as response, instead of relying on PHP's default behavior of just printing anything wherever you wish. This way you can control when you want the response (and therefore the headers) to be sent. This is what many PHP frameworks actually do — you might want to look around and see if you can pick one that suits you, instead of going with raw PHP.
Upvotes: 0
Reputation: 17885
Ideally you should re-arrange your code so that redirects can only happen before any headers are sent. But if you cant, you can just store everything in a buffer and send to the browser once everything has been done:
ob_end_clean();
header('Location: xx');
You might need to have ob_start();
at the top of your script though.
I was going to disagree with the JS solutions but in this day and age I expect it will be fine.
Upvotes: 1
Reputation: 3034
Add on the start of the page: ob_start(); to start buffering or enable buffering in config files. Then everything you output will "wait" for the script to finish or for you to flush the buffer: ob_flush.
So:
ob_start();
//something
ob_end_clean(); //erase output
header('Location: ');
Upvotes: 2