What is the use of header() function in php

❮ PHP Network Reference

Example

Send three HTTP headers to prevent page caching:

// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
?>



...
...


Note: There are options that users may set to change the browser's default caching settings. By sending the headers above, you will override any of those settings and force the browser to not cache!


Definition and Usage

The header() function sends a raw HTTP header to a client.

It is important to notice that the header() function must be called before any actual output is sent!

Syntax

header(header, replace, http_response_code)

Parameter Values

ParameterDescription
header Required. Specifies the header string to send
replace Optional. Indicates whether the header should replace a previous similar header or add a new header of the same type. Default is TRUE (will replace). FALSE allows multiple headers of the same type
http_response_code Optional. Forces the HTTP response code to the specified value


Technical Details

Return Value:Nothing
PHP Version:4.0+
PHP Changelog:PHP 5.1.2: Now prevents that more than one header to be sent at once. This is a protection against header injection attacks

More Examples

Example

Let the user be prompted to save a generated PDF file (Content-Disposition header is used to supply a recommended filename and force the browser to display the save dialog box):

header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='downloaded.pdf'");

// The PDF source is in original.pdf
readfile("original.pdf");
?>


...
...



❮ PHP Network Reference


View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    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:

    Output:

    This will change location of header, i.e. redirect to the URL

    Example 2:

    header("Expires: Sun, 25 Jul 1997 06:02:34 GMT");

    header("Cache-Control: no-cache");

    header("Pragma: no-cache");

    ?>

        

            

    Hello World!

            

            

                print_r(headers_list());

            ?>

        

    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.


    What is the header () function in PHP?

    PHP | header() Function. 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.

    Where is header located in PHP?

    Basically, there are two types of header calls. One is header which starts with string “HTTP/” used to figure out the HTTP status code to send. Another one is the “Location” which is mandatory. replace: It is optional which indicates whether the header should add a second header or replace previous.

    What type of headers that PHP supports?

    The header() is a pre-defined network function of PHP, which sends a raw HTTP header to a client.

    What is the syntax for including a header in PHP?

    The header() function sends a raw HTTP header to a client. It is important to notice that the header() function must be called before any actual output is sent!