Syntax:void header ($header, $replace = TRUE, $http_response_code) Or header (string, replace, http_response_code)
Options: - $header:contains the header line. There are two types of header calls. The first header begins with the string "HTTP /", which is used to define the HTTP status code to send. The second case of the header - "Location:". This is a required parameter.
- $replace:this is an optional parameter. It means that the heading should replace the previous one or add a second heading. The default is True (override). If $replace is False then it forces multiple headers of the same type.
- $http_response_code:This is an optional parameter. This forces the HTTP response code to the specified value (PHP 4.3 and up).
Note.This function prevents multiple headers from being sent at the same time. This is a defense against header attacks after PHP 4.4.Here’s an example of using header() to refresh the current page in PHP:
Example. Inthis example uses the header() function to refresh the web page every 3 seconds.
// Demonstrate using the header() function
// refresh current page
echo
"Welcome to index page
"
;
echo
"Page will refresh in every 3 seconds
"
;
// The function will refresh the page
// every 3 seconds
header (
"refresh: 3"
);
echo
date
(
’ H: i: s Ymd’
);
exit
;
?>
Output: document.createElement (’video’); < / source> Example 2: In this example uses the header() function to redirect a web page to another page.
// Demonstrate using the header() function
// update the current page
echo
"Welcome to index page
"
;
echo
"we will redirect to GeeksForGeeks Official website in 3 second"
;
// The function will be redirected to the official engineerforengineer site
header (
"refresh: 3; url = https://www.engineerforengineer.org/ "
);
exit
;
?>
Output: Links: https://www.php.net/manual/en/function.header.php