PHP redirect mechanism is used to navigate the user from one page to another without clicking any hipherlinks. This will be helpful in such circumstances where the redirect should be done in background. For example, when the user is accessing payment gatway, redirect should automatically be taken place to notify URL using PHP script.
PHP provides predefined function, named header(),for URL redirection. Using this header() function, we need to send location header by specifying URL to which the page should be redirected.
In the above syntax of PHP redirect, we need to replace
Note: Before specifying page URL for location header, we should make sure that the page is exist.
Before executing PHP redirect, we should ensure about, no output is sent to the browser before the line where we call header() function. For example,
echo "PHP Redirect"; header("Location: phppot.com");
This script will display the following warning notice to the browser.
Warning: Cannot modify header information - headers already sent by (...
It is not only applicable for header function, rather for all the PHP functions like set_cookie(), session_start() and etc., whatever can modify the header. For that, we should remove all content which will stop sending location header to the browser.
header("Location: phppot.com"); exit;
ob_start(); // Output Buffering on header("Location: phppot.com"); exit;
This PHP code tutorial was published on May 28, 2013.