Wednesday, March 25, 2009

PHP - redirect page

In this tutorial I will show you how to redirect pages in PHP. You can find all important aspects and code examples about PHP redirecting.

To make a redirection you can use the header() function. This function send a raw HTTP header to the browser. As result the browser will be redirected to the page defined in this new HTTP header. You only have to take care that header() must be called before any actual output is sent. It means you can not use and html tags, echo or print functions. Below is an example how to use redirection in PHP:


<?php

header('Location:http://phpcodetutorials.blogspot.com');

?>

The only thing you have to do is to change the URL inside the header parameter.

However if you write the echo before the redirection you will get an error like this:

Warning
: Cannot modify header information - headers already sent by

To avoid this problem you can use PHP output buffering as follows:


<?php

ob_start();

echo "Test";

header("Location: http://www.php.net");

ob_flush();

?>




No comments: