DEV Community

Cover image for header() function in PHP?
Programming Dive
Programming Dive

Posted on

PHP Header header() function in PHP?

Original and detailed post of this tutorial is located at What is header() function in PHP?

As per php.net documentation, the header function is mainly used to send a raw HTTP header. Well, in the beginning, it is confusing to know the term raw HTTP. In this tutorial, we’ll see what is header() function in PHP, its importance, and its uses.

Use of header() function in PHP

header() function in PHP is used to send raw HTTP header and it must be called before any output is sent to the requester, either by normal HTML tags, blank lines in a file, or from PHP.

The main purpose of header() function is to redirect user from the current/certain page to another URL.

Let’s understand the syntax first and then we’ll see individual parameters.

header ( string $header [, bool $replace = TRUE [, int $http_response_code ]] ) : void

Where,

$header: is the header string which has two special-case header calls. One is the “Location:” and another is the “HTTP/“.
$replace: Optional. It indicates whether the header should replace a previously sent similar header, or add another header of the same type.
$http_response_code: It forces the HTTP response code to the specified value.

Using “Location:”

With “HTTP/” parameter can contain any HTTP status code to send. For example, if we have configured Apache to use a PHP script to handle requests for missing files then we can redirect user to the specified file

<?php
header("HTTP/1.0 404 Not Found");
Enter fullscreen mode Exit fullscreen mode

This will show a 404 page not found page because we have added security restriction for accessing certain pages so that if the user tries to access those page then we’ll certainly show not found page.

Download file using header in PHP

The advantage of header function is that we can create and download files on the fly. This is a very important feature that is needed while working with files related operations in the project.

<?php

// what kind of document to be downloaded
header('Content-Type: application/pdf');

// name the file as document.pdf
header('Content-Disposition: attachment; filename="document.pdf"');

// Read original.pdf file and output it
readfile('original.pdf');
Enter fullscreen mode Exit fullscreen mode

There are basic three steps to download any file at runtime.

  • Define type of file to be downloaded
  • name that file
  • read the content of the file/text
  • Besides other intermediate steps, these are the 3 basic steps which are used to download file on the fly.

    Set HTTP headers to prevent page caching

    PHP is quite famous for its dynamic data. But there are certain situations where we don’t client browser to cached those data (to load website faster of course )

    We can use header() function to override such a situation and add certain parameters that will prevent webpages from being cached.

<?php
header("expires: Wed, 11 Jan 1984 05:00:00 GMT");
header("cache-control: no-cache, must-revalidate, max-age=0");
?>

Here, we have added cache-control: no-cache to make sure that each time browser request for a particular webpage then server must send updated data rather than cached one.

We’ve also added expires parameter to past date represents that web-page is already expired and new data is expected from the client browser.

Conclusion:

Introduced in PHP 4, header function plays an important role while development. May modern frameworks used this function in the core to redirect users to a specific function. Downloading file at runtime, setting cache-control is the of the main features in header() function

I hope that I have covered all the related information about what is header() function in PHP and it’s related uses. If you have any questions Or you think any need for improvement then please comment.

Top comments (0)