Redirect function?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jameso
Forum Newbie
Posts: 7
Joined: Wed Mar 17, 2004 3:31 pm

Redirect function?

Post by jameso »

Is there a function in PHP that will redirect, say, when I'm doing with code.
I know in ASP you can have something like

Code: Select all

<% Response.Redirect("URL_HERE") %>
but I don't know in PHP how to do this sort of thing.

Any ideas anyone?

I was maybe thinking use JavaScript with the window.location function or somehow using a META Refresh.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

header("Location: /foo.php");
exit;

The header must come before any output though, ie only useful if you're not 'sending anything to the user'. You can use output buffering but best not to go there ;)
jameso
Forum Newbie
Posts: 7
Joined: Wed Mar 17, 2004 3:31 pm

before anything?

Post by jameso »

so that function must precede all other code?

I want to send a user to a location after filling out a form, but I have other things, like headers and such, in include files, that have to be loaded.

Will this still work? (I'm trying it out right now but I want to know what problems can arise and how I can correct them)

Thanks!
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

It wil work as long as you don't send output. Output is basically anything that gets sent to the browser/client, such as html and even whitespace (outside of php tags).

So you can do show form->post form->process form->redirect
But if you want to do show form->post form->process form->display something to the user->redirect, then the header won't work (unless you use the previously mentioned output buffering), in this case using a meta-refresh is usually better.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

rajivkrishnang
Forum Newbie
Posts: 5
Joined: Tue Mar 23, 2004 12:18 am

Post by rajivkrishnang »

no echo should work before redirection; thts all;

u can have any no of redirection
jameso
Forum Newbie
Posts: 7
Joined: Wed Mar 17, 2004 3:31 pm

thanks

Post by jameso »

Thanks guys.

I think I get it.
Post Reply