header() function in PHP?
Tags: php
Published: 2020-07-22 05:21:14

Original and detailed post of this tutorial is located at <a href="https://programmingdive.com/what-is-header-function-in-php/">What is header() function in PHP?</a>

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.

<h2>Use of header() function in PHP</h2>

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.


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

Where,

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

<h3>Using “Location:”</h3>

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
<?php
header("HTTP/1.0 404 Not Found");
```

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.

<h2>Download file using header in PHP</h2>
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
<?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');
```

There are basic three steps to download any file at runtime.
<ul>
<li>Define type of file to be downloaded</li>
<li>name that file</li>
<li>read the content of the file/text</li>

Besides other intermediate steps, these are the 3 basic steps which are used to download file on the fly.

<h2>Set HTTP headers to prevent page caching</h2>

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

<h2>Conclusion:</h2>
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.