How to Redirect in PHP

Subodh Poudel Feb 02, 2024
  1. Use the header() Function in PHP to Send URL as HTTP Header to the Browser
  2. Use a Helper Method in PHP to Redirect to Different Page Using header() Function With Status Code
  3. Use Output Buffering in JavaScript to Redirect to Another Page
How to Redirect in PHP

We will demonstrate one way to redirect a page into another page in PHP using the header() function by sending the HTTP header to the browser. This method uses the in-built header() function in PHP, which takes Location as a parameter whose value is the URL of the desired page.

We will also introduce a method to redirect a page into another using a helper function with URL and status codes as parameters and then calls the header() function inside the method.

We will show another way of redirecting into another page using javascript inside echo in PHP using window.location to store the page’s URL to be redirected.

Use the header() Function in PHP to Send URL as HTTP Header to the Browser

We can use the header() function, which takes Location as a parameter. The value of Location is the URL of the desired page, which we need to redirect. Note that the header function should be written above the HTML tags and texts in the file. The header() function does not execute after other data is sent to the browser. It should be the first line of code to be executed.

For exmaple, store the url http://facebook.com in a variable redirect_page. Use the header() function and specify the variable redirect_page in Location in the function. Then, call the die() function.

In the example below, the URL of Facebook is stored in the redirect_page variable. The variable is used in the header function. When the following script is run, the page redirects to the homepage of Facebook. The die() function stops the script from continuing after executing the header() function to prevent unexpected behavior. Please check php manual for information about the header()function. Make sure to place the PHP file inside the local webserver to run it in your browser.

Example Code:

# php 7.x
<?php
$redirect_page = 'http://facebook.com';
header('Location:'  .$redirect_page);
die();
?>

Use a Helper Method in PHP to Redirect to Different Page Using header() Function With Status Code

We will use a helper function redirect to redirect a page to a different page.

Define a method redirect. Take URL and status code as arguments in the function. Write the header() function inside the method. Take URL for Location and status code as arguments for the header() function. Then, call the die() function. The script dies after the execution of header() function as it come across die() function. Outside the function definition, call the redirect function providing the URL http://example.com/ as a parameter. It invokes the function with the URL as the parameter.

In the example below, status code 301 is used to redirect to another page permanently. The script redirects the current page to http://example.com/. Please check MDN Web Docs to know more about the 303 status code.

Example Code:

#php 7.x
<?php
function redirect($url, $statusCode = 301) {
    header('Location: '  . $url, $statusCode);
    die();
}
redirect('http://example.com/');
?>

Use Output Buffering in JavaScript to Redirect to Another Page

We will use JavaScript inside PHP to redirect a page to another page. In the example below, we redirect one page to the Twitter home page.

Write the type of the tag script as text/javascript to write some JavaScript code. Use window.location to store the URL - http://www.twitter.com. Print the whole script tag using echo.

This method is called buffering output. It is a faster method to redirect to another page. Please check PHP official site to know more about buffered output.

Example Code:

#php 7.x
<?php
echo '<script type="text/javascript">
    window.location = "http://www.twitter.com/"
    </script>';
?>
Subodh Poudel avatar Subodh Poudel avatar

Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.

LinkedIn