Refresh a page using PHP - GeeksforGeeks

31 Jul, 2021
Use header() function to refresh a web page in PHP. The HTTP functions are those functions which manipulate information sent to the client or browser by the Web server before any other output has been sent. The PHP header() function sends an HTTP header to a client or browser in raw form. Before HTML, XML, JSON or other output has been sent to a browser or client, a raw data is sent with the request (especially HTTP Request) made by the server as header information. HTTP header provides required information about the object sent in the message body more precisely about the request and response.
Syntax:
void header( $header, $replace = TRUE, $http_response_code )
Or
header(string, replace, http_response_code)

Parameters:
$header: It holds the header string. There are two types of header calls. The first header starts with string “HTTP/”, which is used to figure out the HTTP status code to send. The second case of header is the “Location:”. It is mandatory parameter.
$replace: It is optional parameter. It denotes the header should replace previous or add a second header. The default value is True (will replace). If $replace value is False then it force multiple headers of the same type.
$http_response_code: It is an optional parameter. It forces the HTTP response code to the specified value (PHP 4.3 and higher).
Note: This function prevents more than one header to be sent at once. This is a protection against header injection attacks after PHP 4.4 release.
Below example illustrates the use of header() to refresh current page in PHP:
Example: This example uses header() function to refresh a web page in every 3 seconds.
<?php  // Demonstrate the use of header() function// to refresh the current page   echo "Welcome to index page</br>";echo "Page will refresh in every 3 seconds</br></br>";    // The function will refresh the page // in every 3 secondheader("refresh: 3");    echo date('H:i:s Y-m-d');  exit;?>
Output:
Example 2: This example uses header() function to redirect web page into another page.
<?php  // Demonstrate the use of header() function// to refresh the current page   echo "Welcome to index page</br>";echo "we will redirect to GeeksForGeeks Official website in 3 second";    // The function will redirect to geeksforgeeks official websiteheader("refresh: 3; url = https://www.geeksforgeeks.org/");    exit;?>
Output:
References: https://www.php.net/manual/en/function.header.php
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Picked
PHP
PHP Programs
Web Technologies
Web technologies Questions
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
Comparing two dates in PHP
PHP | Converting string to Date and DateTime
How to fetch data from localserver database and display on HTML table using PHP ?
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
Comparing two dates in PHP
How to fetch data from localserver database and display on HTML table using PHP ?
How to run JavaScript from PHP?