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

Linda Hamilton
Release: 2024-10-27 15:06:29
Original
421 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template