How to use header() function to refresh the page in PHP?

青灯夜游
Release: 2023-04-05 19:12:02
Original
11830 people have browsed it

In PHP we can use the header() function to refresh the page; this function will send the HTTP header to the client or browser in its original form. The following article will introduce to you how to use the header() function to refresh the page. I hope it will be helpful to you.

How to use header() function to refresh the page in PHP?

PHP header() function

PHP header() function converts the HTTP header in its original form Sent to client or browser. Raw data is sent as header information with requests made by the server (especially HTTP requests) before sending HTML, XML, JSON or other output to the browser or client. HTTP headers more accurately provide the information needed about the objects sent in the message bodies of requests and responses.

Basic syntax

header( $header, $replace = TRUE, $http_response_code )
Copy after login

or

header(string, replace, http_response_code)
Copy after login

Parameters:

● $header: It contains the header string. There are two types of header calls. The first case of the header starts with the string "http/" and is used to determine the HTTP status code to be sent. The second case of headers starts with "Location:". This is a mandatory parameter.

●$replace: This is an optional parameter and can be omitted. It is used to indicate that a header should replace the previous one or add a second header; the default value is True (will replace). If the $replace value is False, multiple headers of the same type are forced to be used.

●$http_response_code: This is an optional parameter and can be omitted. It forces the HTTP response code to a specified value (php 4.3 and above).

Note: This header() function prevents multiple headers from being sent at the same time. Following the release of PHP 4.4, this is protection against header injection attacks.

Code example

The following is an example of how to use the header() function to refresh the page in PHP:

Example 1: Use the header() function to refresh the web page every 3 seconds.

<?php 
header("content-type:text/html;charset=utf-8");
echo "页面每3秒刷新一次</br></br>"; 
    
// 该函数每3秒钟刷新一次页面
header("refresh: 3"); 
    
echo date(&#39;H:i:s Y-m-d&#39;); 
  
exit; 
?>
Copy after login

Output:

How to use header() function to refresh the page in PHP?

Example 2: Use the header() function to redirect the web page to another page after 3 seconds Jump to another page

<?php 
header("content-type:text/html;charset=utf-8");
echo "我们将在3秒钟内重定向到PHP中文网!</br></br>"; 
    
// 该函数每3秒钟刷新一次页面
header("refresh: 3; url = http://www.php.cn/"); 
exit; 
?>
Copy after login

Output:

How to use header() function to refresh the page in PHP?

Related video tutorial recommendation: "PHP Tutorial"

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to use header() function to refresh the page in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!