PHP header() function

The PHP header() function is used when we need to send raw HTTP headers. Most of the time, the header() function is used to redirect to another page or URL. For example:

      echo "Welcome to!";
      // block of code to process further
      header('Location: login.htm');

In the above example, since the session variable user is not set, program flow goes to the "else" block and executes the header() function, which redirects to the login.htm page. Because I have already created a login.htm page in the current directory. Therefore, the output of the above PHP example is:

php header function

Since the page has redirected to login.htm in a fraction of a second,  we are directly seeing the login.htm page.

PHP header() Syntax

The syntax of the header() function in PHP is:

header(header, replace, responseCode);

From all three parameters, only the "header" parameter is required. And the other two parameters are optional.

Note: The header parameter is used to specify the header to send.

Note: The replace parameter is used when we need to specify whether the header should replace a previous similar header or add a new header of the same type.

Note: The responseCode parameter is used when we need to force the default HTTP response code to a particular one.

Note: The default value of the replace parameter is TRUE. And the TRUE indicates that the header replaces the previous.

Use PHP header() to Start the Automatic Download of a File

The PHP header() function can also be used to start an automatic download of a file on the client's browser. For example:

   header('Content-Disposition: attachment; filename="myfile.pdf"');

The output of the above PHP example using the header() function to start an automatic download is shown in the snapshot given below:

php header function download

You can also use the Content-Type to indicate the original media type of the resource. That is, the Content-Type representation (in header()) is used when we need to indicate the original media type of the resource (prior to any content encoding applied to send). For example:

header('Content-Type: application/pdf');

If you want to download a file with another name on the client's computer, for example, suppose I have a file named class_details.pdf saved on my server. But I want to make this file available for download on the client's computer system with the name download.pdf. Therefore, here is the code to follow for this purpose:

   header('Content-Type: application/pdf');
   header('Content-Disposition: attachment; filename="download.pdf"');

Now a file named download.pdf will be downloaded on the client computer, whose content will be the same as the content of class_details.pdf, a file saved on my server. Most of the time, this is used by the developer to avoid providing the same name as the file saved on the server.

Use PHP header() to prevent page caching

We all know that PHP scripts are known for their dynamic content. And the dynamic content must not be cached, either by the browser (client browser) or by proxy caches between the server and browser (client browser). Therefore, we need to use some PHP code to prevent caching for some pages or parts of an application written in PHP:

   header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
   header("Cache-Control: no-cache");

Note: Use PHP getallheaders() function to get all HTTP headers.

PHP Online Test

« Previous Tutorial Next Tutorial »

Liked this post? Share it!