Current Location: Home> Latest Articles> How to use header() to implement page redirection

How to use header() to implement page redirection

M66 2025-05-18

In PHP, the header() function is used to send the original HTTP header information. These headers can control the behavior of the browser, such as setting page redirection. Page redirection is usually used when you need to direct the user to a different page, such as redirecting to the user's homepage after logging in, or to a new page under certain conditions.

This article will introduce how to use the header() function in PHP to implement page redirection and show some common examples.

Basic syntax

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

 header(string $header, bool $replace = true, int $response_code = 0): void
  • $header : Passes the HTTP header string to be sent.

  • $replace : If true (default), the same type of value in the current header will be replaced. If false , the header is appended to the header.

  • $response_code : Specifies the HTTP response status code, the default is 0, which means that the status code is not set.

Implement page redirection

To perform page redirection, it can be achieved through the Location header. The Location header indicates that the browser loads the new URL.

For example, the following code redirects the user to https://m66.net/newpage :

 <?php
header("Location: https://m66.net/newpage");
exit();
?>

Code parsing:

  • header("Location: https://m66.net/newpage"); : This line of code tells the browser to jump to https://m66.net/newpage .

  • exit(); : To ensure that the script no longer continues to execute after sending the redirect, the exit() function is usually added. This prevents extra content from being output after redirection.

Set the redirection response code

Sometimes, we may need to send a custom HTTP status code. By default, the Location header uses a 302 Found status code. But you can also explicitly set other status codes.

For example, to use the 301 Moved Permanently status code:

 <?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://m66.net/newpage");
exit();
?>

Code parsing:

  • header("HTTP/1.1 301 Moved Permanently"); : Set the HTTP response status code to 301, indicating that the page has been permanently migrated.

  • header("Location: https://m66.net/newpage"); : Tell the browser to redirect to the new URL.

Use relative URL

In addition to using a full absolute URL, you can also use a relative URL for redirection. In this way, when the user accesses the PHP page, the redirection will be based on the root directory of the current website.

For example, redirect the user to the /newpage page of the current website:

 <?php
header("Location: /newpage");
exit();
?>

In this case, /newpage will be redirected based on the current domain name. If the page currently accessed by the user is https://m66.net/oldpage , the redirected URL will be https://m66.net/newpage .

Prevent output from redirection

The header() function must be called before outputting anything. This is because the HTTP header information must be sent before other content on the page, such as HTML. If the page has started outputting content, the header() function will not work properly and an error will be triggered.

To avoid this problem, make sure the call to the header() function is at the beginning of the script, or before sending any HTML or other output.

If you accidentally have output (such as HTML tags or spaces) before the output, you can use ob_start() to buffer the output. For example:

 <?php
ob_start();
header("Location: https://m66.net/newpage");
exit();
ob_end_flush();
?>

ob_start() starts the output buffer, while ob_end_flush() clears the buffer at the end of the script and sends the output.

Conclusion

PHP's header() function is a very powerful and commonly used tool, especially suitable for implementing page redirection. By correctly setting the Location header, combined with the appropriate HTTP response status code, you can control the browser's redirect behavior, whether it is a temporary jump or a permanent jump.

Remember to avoid any output when calling header() to ensure that the redirection can be executed smoothly. If needed, you can also use output buffering to avoid problems caused by output.

Hope this article will be helpful for you to understand page redirection in PHP!