Home >Backend Development >PHP Tutorial >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!