Header are used to send a raw HTTP reader.During page upload a header is called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. There are two special-case header calls.

HTTP/  header

The first is a header that starts with the string HTTP/, which will be used to figure out the HTTP status code to send. For example, if you have configured Apache to use a PHP script to handle requests for missing files, you may want to make sure that your script generates the proper status code.

Example of HTTP/ header for missing files

<?php
    header("HTTP/1.0 404 Not Found");
?>

Location: header

The second special case is the Location: header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless the 201 status code has already been set. 

Example of Location: header

<?php
    header("Location: http://www.brenkoweb.com/");
    exit;
?>

WWW-Authenticate: header

The optional replace parameter indicates whether the header should replace a previous similar header, or add a second header of the same type. By default it will replace, but if you pass in FALSE as the second argument you can force multiple headers of the same type.

Example of WWW-Authenticate: header

<?php
    header("WWW-Authenticate: Negotiate");
    header("WWW-Authenticate: NTLM", false);
?>

Content-Type and Content-Disposition: headers

If the user want to save the data, sending, such as a generated PDF file, you can use the Content-Disposition header to supply a recommended filename and force the browser to display the save dialog.

<?php
    header("Content-Type: application/pdf");
    header("Content-Disposition: attachment; filename='download.pdf'");
    readfile("file.pdf");
?>

 

›› go to examples ››