PHP | header() Function

01 Aug, 2021
The header() function is an inbuilt function in PHP which is used to send a raw HTTP header. 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 send a 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 request (especially HTTP Request) made by the server as header information. HTTP header provide 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 )
Parameters: This function accepts three parameters as mentioned above and described below:
$header: This parameter hold 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).
Return Values: This function doesn’t return any value.
Example 1:
<?php// PHP program to describes header function  // Redirect the browserheader("Location: https://www.geeksforgeeks.org");  // The below code does not get executed // while redirectingexit;  ?>
Output:
This will change location of header, i.e. redirect to the URL
Example 2:
<?php// PHP program to describes header function  // Set a past dateheader("Expires: Sun, 25 Jul 1997 06:02:34 GMT");header("Cache-Control: no-cache");header("Pragma: no-cache");?>  <html>    <body>        <p>Hello World!</p>              <!-- PHP program to display        header list -->        <?php            print_r(headers_list());        ?>    </body></html>
Output:
Hello World!

Array ( 
    [0] => X-Powered-By: PHP/7.0.33 
    [1] => Expires: Sun, 25 Jul 1997 06:02:34 GMT 
    [2] => Cache-Control: no-cache 
    [3] => Pragma: no-cache 
)

The above example helps to prevent caching by sending header information which override browser setting to not-cache.
Note: The header() functions is used multiple time in the example as one header is allowed to send at a time (since PHP 4.4) to prevent header injection attacks.
Uses:
Change page location
Set timezone
Set caching control
Initiate force download
Send HTTP Status
Reference: http://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.
PHP-function
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to convert array to string in PHP ?
PHP | Converting string to Date and DateTime
Comparing two dates in PHP
How to receive JSON POST with PHP ?
How to get parameters from a URL string in PHP?
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?