PHP header() Function

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

<?php
   if(isset($_SESSION['user']))
   {
      echo "Welcome to fresherearth.com!";
      // block of code, to process 'user'
   }
   else
   {
      header('Location: login.htm');
      exit();
   }
?>

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

php header function

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

PHP header() Syntax

The syntax of 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 previous similar header or should add new header of same type.

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

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

Use PHP header() to Start 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:

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

The output of above PHP example on 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 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 other name on the client's computer. For example, let us suppose, I have a file named class_details.pdf saved on my server. But i want to make this file, to download on the client's computer system with name download.pdf. Therefore, here is the code to follow, for this purpose:

<?php
   header('Content-Type: application/pdf');
   header('Content-Disposition: attachment; filename="download.pdf"');
   readfile('class_details.pdf');
?>

Now a file named download.pdf will be downloaded on the client computer whose content will be 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 of the file, saved on the server.

Use PHP header() to Prevent Page Caching

As we all knows that, PHP scripts known for its dynamic content. And the dynamic content must be not cached, either by the browser (client browser) or by proxy caches between server and browser (client browser). Therefore, we need to use some PHP code to prevent cache for some page/part of an application written in PHP:

<?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 »