Forum Moderators: coopster

Message Too Old, No Replies

redirect to a new/different page

         

surrealillusions

4:05 pm on Jan 28, 2008 (gmt 0)

10+ Year Member



Hi,

Been looking at ways to redirect a user to another page. I've seen the location header php function thingymigig, but, it seems you cant do anything before it otherwise it throws an error in your face.

How do you redirect a user to another page after they have sent you an email or logged in using a form or something.

Do you use a javascript redirect or..do you stick the php script that processes the form inside a webpage, where you want the end result to be displayed like "you have succesfully logged in" for example? So the user doesn't get a blank white page with the message across the top left with no other way of getting back to your site except pressing the back button or re-typing in your website address...

:)

jatar_k

4:09 pm on Jan 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> it seems you cant do anything before it otherwise it throws an error in your face.

you only can't have any output to the browser before you use it

you can do anything else that you need to do though

phranque

11:55 pm on Jan 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



the first part of any HTTP response must be a valid HTTP response header.

surrealillusions

12:27 pm on Jan 29, 2008 (gmt 0)

10+ Year Member



phranque - what do you mean by that?

:)

PHP_Chimp

1:43 pm on Jan 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For the long and boring answer check out the W3C's HTTP/1.1 headers [w3.org].
The short answer is -
If you look at the actual headers sent when you receive a page you get something like -

HTTP/1.1 200

As the very first thing. This says that the page was found and sent.
If you want to redirect a person then you need to send a redirection header. The 3xx headers redirect. 301 and 302 are the ones that get used the most.
However all of this is slightly academic, as it doesnt actually matter as PHP does all of the work for you.

If you use the header('Location: absolute URL'); then PHP sends a 302 header with the location. For HTTP/1.1 a header must be an absolute url (seeing as most browsers are built to deal with our (webmasters) inadequacy's they accept relative url's as well, however this is incorrect from the W3C [w3.org]).
So if you want to be able to have a form that sends information to you then redirect people you have a number of choices.
1) <form action="some_other_page">
On some_other_page do whatever you want with the information then use header('Location: ...'). As there is no HTML on some_other_page you dont need to worry about where the header is placed in the code, as this will be the only thing sent to the browser.
2) redisplay on the same page


if($_POST['submit']) {
// do whatever with the data
header('Location: ...');
}
else {
// display the page
}

surrealillusions

4:41 pm on Jan 29, 2008 (gmt 0)

10+ Year Member



ok..thanks..

makes a bit more sense

:)