In PHP you can redirect a user to another page using the
header() function.
Code:
<?php
header('Location: http://www.thesunmagazine.org');
?>
http://www.php.net/header
This function is used to modify the HTTP response headers sent out from the server. For this example the 'Location' header is modified, the browser then picks up on the modification and makes a new request from the new location specified.
Because this function modifies the HTTP headers, it may not be included
after output has started from the script.
For example:
Code:
<?php
echo 'Hello, world!';
header('Location: http://www.thesunmagazine.org');
?>
This throws a warning-level error saying something to the effect of headers cannot be modified, output already started at line x in file z.
This, on the other hand, is valid because it uses output control functions to buffer script output.
Code:
<?php
ob_start();
echo 'Hello, world!';
header('Location: http://www.thesunmagazine.org');
ob_end_flush();
?>
Output from the script is written to the buffer, therefore, when the script reaches header() it is able to make HTTP header changes since nothing has yet been sent to the browser.
Output buffering increases resource consumption slightly because extra memory has to be allocated for the buffer.
More on output buffering:
http://www.php.net/outcontrol
Also, as a side note, it doesn't make any sense to have need of outputting anything before redirecting a user with the 'Location' header since all output will be lost anyway. However, this does come in handy for other functions or header modifications such as session_start() or setcookie() both of which make modifications to the outgoing HTTP headers. If you find you're in need of showing the user output
before redirection, this is better done on the client-side with JavaScript.