HomeBest HostingHow to use the PHP header function

How to use the PHP header function

In this tutorial, we’ll guide you through using the PHP header feature to send raw HTTP headers.

PHP header function

You can use the header function in PHP to set and send HTTP headers to the end user. HTTP headers are useful because they allow both the client and the server to transmit information with a request or a response. Additionally, you can use these headers to control caching, specify a content type, redirect users, and more.

There is a wide range of accepted headers that you can use, so I recommend that you take the time to learn them if you intend to become a web development expert. We will cover some examples in this tutorial.

This tutorial will guide you through the syntax of the header function, some examples of using the function, and a common error code associated with using the header function.

The header function supports three parameters, but only the first parameter is required.

It is essential that you use the header function before sending any output to the end user. If you try to use the function after exiting, you will probably see an error.

HEADER is where you specify the header string to send. This parameter is required and will result in an error if it is not specified.

There are two special headers that you can use with this feature. I will explain them quickly below.

The first case involves the use of a header that begins with HTTP/. You can use this to specify the status code to send. E.g, HTTP/1.1 404 Not Found will result in a 404 error page when the user accesses the page.

The second case involves starting the header with location:. Depending on your location, you can specify a redirect URL. E.g, Location: https://google.com will result in a 302 redirects to Google, unless 201 or 3XX status code already sent.

Replace is a optional parameter and default to TRUE if not specified. Whether PHP should replace the header if it already exists. stating FAKE will allow multiple headers of the same type.

http_response_code is an optional parameter and can be used to force a specific response code.

There are many different headers that you can set up using the header function in PHP. In this section, we'll show you some of the different ways you can use headers.

Return a specific status code

You can specify a specific status code to be returned as a header. There are many status codes that you can send to a browser. For example, the following example will send a 404 status code to your browser. use $_SERVER["SERVER_PROTOCOL"]we can also make sure that the correct protocol is used, for example HTTP/1.0.

The example below is the response we received from our local web development server.

404 Header status code

Download files

In this example, we show you how to specify a file for a user to download.

First we indicate the type of content, which is a PDF file. You can specify other types of content, such as images, audio, text, video, and other applications.

Second, we use a Content-Disposition header to indicate that the file should be downloaded and not displayed in the web browser. Finally, in the same header, we specify filenamewhich will be the name of the file once it has been downloaded.

Finally, we use readfile function to read a file and buffer it. The setting for this feature should be the location and name of the file that you want to be available for download.

The code below is very simple, which you may want to extend if you want to use it on a production server.

The headers below come from our local web development server. A file was downloaded when I accessed the PHP script.

Download PDF headers

Using headers to control caches is a valuable tool, especially if you're dealing with proxy or CDN caches. I recommend reading about the various cache control headers, as there is quite a lot to cover.

The first header uses Cache-Control header with no-cache, no-store, must-revalidate direction.

no-cache indicates that the browser must validate the response with the source server each time it is reused. However, it allows the answer to be cached.

no-store indicates that the answer should not be cached.

must-revalidate indicates that if the answer becomes obsolete, it must be validated before it can be used again.

Our second header specifies a expires header set to a past date. Having a past date means that the answer is always considered outdated and therefore unlikely to be cached.

Our browser can see that our cache and expiration control headers exist with their data.

Caching headers

Redirect users

Our last example is using headers to redirect the user to another page. By default, the status code for using this header is 302 Temporary forwarding. However, you can change this by indicating a different code as the third parameter.

In the example below, we use the location header to redirect anyone accessing our PHP script to the Google search engine.

As you can see in the screenshot below, our script redirected us to Google using 302 temporary redirect status code.

redirect header

You may receive the following error when you try to use the header function. The warning indicates that there is a problem somewhere in your PHP files. Diagnosing the problem can be simple or complicated.

If you receive the above error, it means that the data has been sent and the headers have been sent. Exit can be intentional or unintentional. For example, warnings, error messages, or even spaces outside of PHP tags can cause headers to be sent prematurely. Functions such as printing, echo, and other data extraction functions may also cause headers to be sent.

The error message should contain enough information to locate the culprit for sending the headers in advance. Fixing it can be as easy as removing the offending code or moving the header logic before starting production.


I hope by now you have a basic understanding of how you can use the header function in PHP. We have covered some useful examples and the most common error you can have with this function.

There are many more useful functions that you can use in PHP to help build a useful program. For example, you may also find features like sleep useful for controlling the execution of your scripts.

If we can improve this tutorial, feel free to let us know by leaving a comment below.

Dedicated Server
Dedicated Serverhttps://www.winteringhamfields.com
Hi, By Profession I am an Injury Attorney who handles accident cases of cars with no insurance. I took College Classes online to get a degree in game design too.

Most Popular

Recent Comments