PDA

View Full Version : redirecting at the middle of script. possible?


aZa
01-29-03, 19:57
Is it possible to make a redirection in the middle of the script work when all headers are long-time sent? :D

What I need to do is to check at the middle of the page some variable's value and depending on it automatically redirect current page to other one. Any solutions?

TIA.

arfman
01-30-03, 05:12
you can't do that, php is a server script, if the headers are already send, the client is beginning to get the page.

Why don't you test your variables in the top of the page ?

Another solution may be to put the headers in a string variable, to test for your redirection and to write the string after that.

And the last solution is to put a javascript redirection in the middle of your page ... but it's not the great solution :(

bcyde
01-30-03, 12:11
Like arfman said, if you have already sent the headers then you will not be able to redirect via PHP. If it's possible process all your calculations before outputting anything and work from there.

If you'd like to provide more info no what you're working with maybe a more in depth solution can be given.

-b

aZa
01-30-03, 14:43
Well, it's kind a too complicated to describe the exact problem I've met with in coding but I do really need some automatic redirection!.. What about javascript's one? Could you please provide more info on this subject how can I do this? And why this is not that good method for problem solution?

TIA.

bcyde
01-30-03, 15:06
Well the javascript function you want is window.location
and it's usage is just

window.location = "http://www.yoururl/";

so I guess if you really must do it you can probably just echo out a script that just does:

if ($bRedirect) //if we need to redirect
{

?>

<script language="javascript">
window.location = "http://www.yoururl/";
</script>

<?

}

or if you don't like breaking in and out of script

if ($bRedirect) //if we need to redirect
{
echo "<script language=\"javascript\">\n";
echo "window.location = \"http://www.yoururl/\";\n";
echo "</script>\n";
}

That should do it I think, I haven't tested it out, but if you have problems
just leave another message here. As to why it's bad, well preferably stuff like redirects you want to keep out of the hands of the client. For example what happens if, say a user has javascript disabled on their browser, or has a browser that doesn't support a specific javascript feature you're using?

Also, unless there's something really weird going on decisions like redirecting or denying access should be done first before any other work is done so that you don't do extra processing that will just be discarded.

Hope this helps,
-b

aZa
01-30-03, 15:31
Thank you, works fine for me! Just added a little notice with the link for those who won't be able to redirect automatically.

arfman
01-31-03, 04:48
why isn't it the great solution ?

because if you make a php redirection, the client never get the first page, and don't have to download a page with a javascript redirection.
A php redirection is on the server, which don't upload you the wrong first page but the second you have to be redirected.
With a javascript redirection, you download the first page, your browser intercept the javascript redirection code and ask for it on the server and so you download 2 pages.

The other problem is that your are browser "addict", if the browser has javascript disabled the redirection doesn't work, in php you don't have to hurry about that :)

aZa
01-31-03, 17:05
Yes. I've got it. But even such software as these boards are written that way that you first get the page with information about new message adding from you and only after this automatically redirecting to the actual updated thread ... using javascript, I guess.

I wanted something like this. And I've got it! ;) Thanks!