Register Login

Redirect to Another Page in PHP

Updated Jun 21, 2023

The redirection from one page to another is one of the common practices among PHP users. PHP is a popular, open-source, server-side, general-purpose scripting language suited for web development. In this article, we will discuss how to redirect in PHP.

PHP Redirection

In PHP, users can do redirection using the header() function. The header() function is an inbuilt function of PHP. Users can use this to send a basic Hyper Text Transfer Protocol (HTTP) header to the client. It simply redirects a user to a different page.


header($header, $replace, $http_response_code)

Following are the parameters used in this function

This function accepts three parameters as mentioned above and described below:

  • $header: Users can use this parameter to hold the header string.
  • $replace: Users can use this parameter to hold the replace parameter, which shows the header must replace the previous similar header file or add another header file having the same type. This parameter is an optional one.
  • $http_response_code: This parameter hold the HTTP response code.

The code snippet will help users understand how PHP redirection works:

	// Redirect browser
	header("Location:" , true, 301);



Here, we have used a PHP code with a header() function to redirect the The above example will redirect users to the page mentioned within the header function.

Status Codes:

In PHP, the default code is 302, which implies a "temporary" redirect.

Other Redirect codes

  • 301, which implies a "Permanent" redirect
  • 302, which indicates a "Found"
  • 303, which indicates a "See Other"
  • 307, implies a "Temporary Redirect (HTTP/1.1)."

The PHP's "Location"-header uses the HTTP 302 redirect code. It implies a "temporary" redirect. Users, especially, must not use this code in their program because they must consider 301 as a "permanent redirect" or 303 as an "other."


Users must add the exit() or die() function which is mandatory.


The difference between an HTML redirect and a PHP redirect is that HTML is client-side whereas PHP is server-facing. Because of this key distinction, a PHP redirect will provide quicker and more secure navigation from one page to another.

However, you should keep in mind that, while there are various benefits associated with implementing a PHP redirect, they could be dangerous if they’re done incorrectly. Therefore, you must follow the proper steps to set one up.

What is the use of die() and exit()?

Users can use the exit() function to stop the execution of the program. They can use the die() to throw an exception and halt the execution.

Warning msg: warning like header already sent

Often while using the header function, they will get a warning message "Warning like header already sent." It indicates resolving the issue where users do not use echo or print before sending headers. Users can use die() or exit() in the header function.

Using the header function with ob_start() and ob_end_flush()

The ob_start() constructs an output buffer, and ob_end_flush() deletes the topmost output buffer and outputs of its contents.

Code Snippet

	ob_start(); //users must add this in the first line of the PHP page
	header('Location: target-page.PHP');
	ob_end_flush(); //users must add this in the last line of the PHP page



We have used the ob_start() and ob_end_flush() functions. We can pass the callback function to process the contents of the buffer before it gets deleted from it. The ob_end_flush() deletes the topmost output buffer.

Often the PHP compiler catches the output in another output buffer. If it does not find other output buffers, it directly sends the output to the browser.

PHP Redirect v.s. Javascript or Jquery redirect

While working with PHP redirect, users can easily and quickly work on the server end.

But while working with Javascript or Jquery redirect, it is slow, and the Client end often blocks by stopping JavaScript execution in the browser.


We hope this article has served its purpose of redirection in PHP. Using the PHP header function, users can easily and efficiently redirect the end-users to the correct page or URL. They can also use the die() or exit() function to stop or throw an exception in the PHP program.