Redirect URL in PHP
Header function
This is very simple way to redirect a web page to another web page using PHP at server side. To redirect your site visitors to a new page or new website, you just need to add a header function is first line of the web page. It works almost all browser.
Syntax
<?php Header("Location: Web page address"); ?>
Example of redirect
<?php /* Redirect browser from current web page to another */ Header("Location: another-page.php"); ?>
It is extremely important that you understand that the header() function must be called before any actual output is displayed to the browser.
Header function must be first of you web page because no actual output is sent, as shown below
Example
<?php echo "Hello world"; echo "ptutorial.com"; header("Location: new-page.php"); exit; ?>
Note: Make sure all the actual code after the header function not get executed so don’t write anything after header function.
Absolute / relative path
If the destination web page is on the other server include complete URL like that http://www.ptutorial.com.
Example
<?php header("Location: http://www.ptutorial.com"); exit; ?>
You can also use relative path for redirection like below shown.
Example
<?php header("Location: ../new.php"); exit; ?>
Redirect after particular time
Redirect a webpage to another web page after defined time, use below syntax.
Example
<?php header( "refresh:5;url=http://www.ptutorial.com" ); exit; ?>
Redirect with proper message
Redirect a webpage to another web page after defined time and proper message like “you are redirected after 5 second or any other”, use below syntax
Example
<?php header( "refresh:4; url=http://www.ptutorial.com" ); echo "You will be redirected after 4 second"; ?>
Use of redirect?
- Redirect after form submission
- Drop down menus
- Pull down menus
If you want to learn more about functions and function library please visit
php tutorial

Umar Farooque Khan

Latest posts by Umar Farooque Khan (see all)
- Check whether image exists on remote server - February 11, 2019
- Limit Select2 to one selection per optgroup with multiple selection - February 10, 2019
- MySQL Select query to validate email - February 9, 2019