Sign in Registration
ArticlesDownloadServicesAudioContacts
ruen

HTTP response (server) headers - how to send, receive or remove them in PHP

HTTP headers are very important for the exchange of information between the browser and the server. They are of two types - request headers and response headers. This article covers response headers.

http-response-headers-send-receive-delete-php

How to send HTTP response headers

Since the response headers are sent by the server, it is necessary to use programming language tools, for example, PHP facilities . It has special functions for working with headers. To send the header - header function.

You can only send headers if no data has been sent yet. Before calling this function, there should be no HTML tags, empty lines, etc., including in include files. That is, the headers are sent first, and then the data itself.

Examples of sending headers to PHP to the browser:

  header ('HTTP / 1.1 404 Not Found');
header ('Status: 404 Not Found');
header ('Location: https: // example.com');  

How to get HTTP response headers

The following functions are available for this:

You can also use the CURL library. How to get response headers using CURL? Example:

  if ($ curl = curl_init ('https://example.com')) {
curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ curl, CURLOPT_NOBODY, true);
curl_setopt ($ curl, CURLOPT_HEADER, true);
$ out = curl_exec ($ curl);
echo $ out;
curl_close ($ curl);
}  

How to remove HTTP response headers

Headers can be removed using a special function - header_remove . First you need to check if headers have been sent:

  if (! headers_sent ()) {
    header_remove ('Content-Type');
}  

As you can see from the article, it is very easy to work with HTTP response headers on the server side.

Comments (0)
For commenting sign in or register.

Latest articles

Popular sections

Communication

Login to the web version
Android app:
Available on Google Play

Share this

Subscribe to

YouTube