Home  >  Article  >  Backend Development  >  How to Delay Page Redirects in PHP Using the `header()` Function?

How to Delay Page Redirects in PHP Using the `header()` Function?

Linda Hamilton
Linda HamiltonOriginal
2024-10-27 15:06:29339browse

How to Delay Page Redirects in PHP Using the `header()` Function?

Delaying Page Redirects in PHP

Redirecting a web page after a specified time interval is a useful technique for controlling the user experience. PHP offers a convenient function to achieve this functionality.

The header() Function

PHP's header() function sends a header to the client browser. One of its parameters allows you to specify a time delay for page redirection. The syntax is as follows:

<code class="php">header("refresh:seconds;url=destinationURL");</code>

Redirecting After 5 Seconds

To redirect a page after 5 seconds, for example, you would use the following code:

<code class="php">header("refresh:5;url=wherever.php");</code>

This header must be sent before any output is rendered on the page. Failure to do so will result in an error.

Considerations

Keep in mind that the header must be called before any output is sent, including HTML tags, blank lines, or data from external files. Calling include, require, or other functions that output content before header() can cause problems.

The above is the detailed content of How to Delay Page Redirects in PHP Using the `header()` Function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn