PHP Redirect

by Vincy. Last modified on July 2nd, 2022.

PHP redirect mechanism is used to navigate the user from one page to another without clicking any hyperlinks. This will be helpful in such circumstances where the redirect should be done in the background.

For example, when the user is accessing the payment gateway, the redirect should automatically be taken place to notify the URL using a PHP script.

PHP provides predefined function, named header(),for URL redirection. Using this header() function, we need to send the location header by specifying the URL to which the page should be redirected.

Unlike JavaScript redirect where there are several ways to handle URL redirect works based on the browser, PHP avoids such confusion and has a header() function to create the same effect in all browsers. For that only, we have concluded with the JavaScript redirect article that server-side redirect is preferable.

PHP Redirect Syntax

header("Location: target-url");

In the above syntax of PHP redirect, we need to replace it with a valid URL to which we want to move. We can specify either an absolute URL or a relative URL for this location header. If we specify a relative URL, it will search for the page in our domain where we exist.

Note: Before specifying the page URL for the location header, we should make sure that the page exists.

Caution before Redirect

Before executing PHP redirect, we should ensure about, no output is sent to the browser before the line where we call the header() function. For example,

<?php
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 the header function, but 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 that will stop sending location header to the browser.

Possible Ways of Sending Output

  • HTML content like text or tags.
  • Unnecessary white spaces before PHP delimiters.
  • PHP error or warning notices that occur before calling redirect.
  • PHP print statements, like, echo(), print().

Safety Measures from output being Sent before PHP Redirect

  • Since HTML content should be sent before the redirect, we can separate PHP logic from HTML content.
  • For being on the safe side we can put the exit command after the redirect statement of the PHP file. For example,
    <?php
    header("Location: phppot.com");
    exit();
    ?>
    
  • We can enable PHP output buffering to stop sending output to the browser and store it in a buffer instead. For example,
    <?php
    ob_start(); // Output Buffering on
    header("Location: phppot.com");
    exit();
    ?>
    
Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page