What is the purpose of the "header ()" function?

7

In my question about HTTP protocol I understand that HTTP requests have a format that is described by the protocol itself.

However, in PHP there is a function that sends data to HTTP which is the header() function and I did not quite understand its purpose, even reading the manual I was left with doubts that can be clarified with a canonical response.

Questions

  • What is the purpose of the header() function?
  • Does header() have any relation to the HTTP protocol?
  • When the header() function should be used?
  • In order to facilitate my understanding, I would like practical examples if possible.

        
    asked by anonymous 07.01.2017 / 18:59

    3 answers

    8
      

    What is the purpose of the header () function?

    Just mount a header for the HTTP packet that will be transmitted on that request. By default it generates a basic with the minimum necessary, if you want to customize you can use this function.

      

    Does the header () function have any relation to the HTTP protocol?

    Totally, it only makes sense for this.

      

    When should the header () function be used?

    Whenever you need to do something that is not the ordinary. You can send a specific error ( HTTP/1.0 404 Not Found ), it can indicate the type of content that will be sent ( Content-Type ), expiration ( Expires ), work with authentication ( WWW-Authenticate: Basic realm="realm" ), and perhaps one of the most used in practice , redirect to other content ( location ). But anything that allows the protocol can be used, even header extensions.

    Documentation .

    Examples:

    header('HTTP/1.1 301 Moved Permanently');
    header('Location: http://www.example.com');
    
    header('HTTP/1.1 503 Service Temporarily Unavailable');
    header('Status: 503 Service Temporarily Unavailable');
    header('Retry-After: 60');
    
    header('Content-Type: image/png');
    
    header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
    header('Cache-Control: no-store, no-cache, must-revalidate');
    header('Cache-Control: pre-check=0, post-check=0, max-age=0');
    header ('Pragma: no-cache');
    
    header('Content-Disposition: attachment; filename=' . urlencode($file));   
    header('Content-Type: application/force-download');
    header('Content-Type: application/octet-stream');
    header('Content-Type: application/download');
    header('Content-Description: File Transfer');            
    header('Content-Length: ' . filesize($file));
    echo file_get_contents($file);
    
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="plain_text_file.txt"');
    
    
    header('WWW-Authenticate: Basic realm="The Realm"');
    header('HTTP/1.0 401 Unauthorized');
    
        
    07.01.2017 / 19:10
    5

    The header of an HTTP request / response is a key / value list.

    1 - Basically this function allows you to add values, or update values that already exist in the response header by default (defined by the server).

    2 - Yes, its function is to allow you to edit the header of an HTTP response.

    3 - When you need to enter / change some value of the response header. This can be useful for several things, for example: Force redirect; Force download content; Return a specific status; etc.

    link

        
    07.01.2017 / 19:11
    2

    Just by reinforcing what has already been said, the header function assembles the http response header that will be sent to the browser.

    An addendum to the header function is that it has a second optional parameter, called $replace . If you used as false , the header could be repeated, as in the example below:

    header('WWW-Authenticate: Negotiate');
    header('WWW-Authenticate: NTLM', false);
    
        
    10.01.2017 / 15:23