How to Redirect a Web Page with PHP

This short snippet will show you multiple ways of redirecting a web page with PHP.

So, you can achieve redirection in PHP by following the guidelines below.

Watch a course Learn object oriented PHP

Using the header() Function

This is an inbuilt PHP function that is used for sending a raw HTTP header towards the client.

The syntax of the header() function is as follows:

header( $header, $replace, $http_response_code )

Also, it is likely to apply this function for sending a new HTTP header, but one should send it to the browser prior to any text or HTML.

Let’s see how to redirect a web page using the header() function:

<?php

header('Location: //www.w3docs.com');
// or die();
exit();

?>

As you can notice, exit() is used in the example above. It is applied to prevent the page from showing up the content remained (for instance, prohibited pages).

Also, you can use the header() function with ob_start() and ob_end_flush(), like this:

<?php
ob_start(); //this should be first line of your page header('Location: target-page.php');
ob_end_flush(); //this should be last line of your page

Using a Helper Function

Here, we will demonstrate how to use a helper function for redirecting a web page. Here is an example:

<?php

function Redirect($url, $permanent = false)
{
  header('Location: ' . $url, true, $permanent ? 301 : 302);
  exit();
}
Redirect('//www.w3docs.com/', false);

All HTTP status codes are listed at HTTP Status Messages

Note that this function doesn’t support 303 status code!

Let’s check out a more flexible example:

<?php

function redirect($url, $statusCode = 303)
{
  header('Location: ' . $url, true, $statusCode);
  die();
}

In some circumstances, while running in CLI (redirection won’t take place) or when the webserver is running PHP as a (F) CGI, a previously set Statusheader should be set to redirect accurately.

Here is an example:

<?php

function Redirect($url, $code = 302)
{
  if (strncmp('cli', PHP_SAPI, 3) !== 0) {
    if (headers_sent() !== true) {
      if (strlen(session_id()) > 0) {
        // if using sessions
        session_regenerate_id(true); // avoids session fixation 	attacks
        session_write_close(); // avoids having sessions lock other requests
      }

      if (strncmp('cgi', PHP_SAPI, 3) === 0) {
        header(sprintf('Status: %03u', $code), true, $code);
      }

      header('Location: ' . $url, true, preg_match('~^30[1237]$~', $code) > 0 ? $code : 302);
    }
    exit();
  }
}
?>

JavaScript via PHP

Here, we will provide you with an alternative method of redirection implementing JavaScript via PHP. In JavaScript, there is a windows.location object that is implemented for getting the current URL and redirecting the browser towards a new webpage. This object encompasses essential information about a page (for example, href, a hostname, and so on).

This is how to redirect a web page using window.location:

<!DOCTYPE html>
<html>
  <head>
    <title>window.location function</title>
  </head>
  <body>
    <p id="demo"></p>
    <script>
      document.getElementById("demo").innerHTML = "URL: " + window.location.href + "</br>";
      document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + "Hostname: " + window.location.hostname + "</br>";
      document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + "Protocol: " + window.location.protocol + "</br>";
    </script>
  </body>
</html>

To conclude, let’s assume that in this short tutorial, we provided you with multiple methods to redirect a web page with PHP. Also, you can find information on how to redirect web pages with HTML, JavaScript, Apache and Node.js.