CSC 2720 Building Web Applications Getting and Setting HTTP Headers (With PHP Examples)
Outline What kinds of data are embedded in the HTTP request/response headers? How useful could these data be? What can we achieve by setting HTTP response header? PHP APIs for getting headers from HTTP request PHP APIs for setting HTTP response headers Examples
Introduction The data in the HTTP header contains info about client/server and the data embedded in the HTTP body. HTTP/ OK Date: Mon, 23 May :38:34 GMT Server: Apache/ (Unix) (Red-Hat/Linux) Last-Modified: Wed, 08 Jan :11:55 GMT Etag: "3f80f-1b6-3e1cb03b" Accept-Ranges: bytes Content-Length: 438 Connection: close Content-Type: text/html; charset=UTF-8 Body of the contents goes here … The header section of a HTTP response
HTTP Request Headers You can find out more about your client. For examples accept, accept-encoding, accept-language, accept-charset : Content types, compression schemes, languages, and character sets that the web client can handle. user-agent : Info about the web client and operating system referer : The URL of the webpage that "brings" the client to the requested page cookie : Cookies
Obtaining HTTP Header Fields Header fields of the current request is stored in $_SERVER. For a complete list of predefined names, please refer to Some of the useful info include $_SERVER['HTTP_USER_AGENT'] Contains info about the web client $_SERVER['HTTP_REFERER'] URL of the page (if any) which referred the web client to the current page. (May not be reliable) Cookies should be access through $_COOKIE instead.
HTTP Response Headers You can modify the HTTP response header to Redirect the web client to another URL Send a different HTTP status code Tell the web client whether to cache the current document or not Tell web client what language is used in the current document Change the content type of the current document You can use PHP to dynamically create text file, CSV file, image, etc. Requesting the web client to download another file. Set cookies (but in PHP, cookies should be set through $_COOKIE instead.)
Examples of HTTP 1.1 Response Headers Cache-Control Tells all caching mechanisms from server to client whether they may cache this object. To tells a client not to cache the requested resource, set the its value to no-cache. Content-Language The language the content is in Content-Type The MIME type of the content being returned
Examples of HTTP 1.1 Response Headers Expires The time at which document should be considered as out- of-date Last-Modified The time in which the requested resource was last modified. Location To redirect the web client to a new URL Set-Cookie The cookies that browser should remember.
Functions for Dealing with Header Fields in the HTTP Response header() Set a raw HTTP header headers_list() Return a list of headers to be sent to the web client The list is a numerically indexed array headers_sent() Return FALSE if no HTTP headers have already been sent or TRUE otherwise
<?php header('Location: exit(); // Return immediately ?> Redirecting the web client to another URL header() must be called before any actual output is sent! When a web client access the above file, it will go to immeidately. <?php header('Location: exit(); ?> Even with only one empty space sent, subsequent calls to header() will fail.
<?php header("HTTP/ Not Found"); ?> Send a different HTTP status code Creating a custom-made page to display the error message when a requested resource cannot be found by the server. You also need to configure the web server to use your custom-made page.
<?php // HTTP/1.1 header("Cache-Control: no-cache, must-revalidate"); ?> Tell the web client whether to cache the current document or not For the format of HTTP header fields, please refer to HTTP/1.1: Header Field Definitions The above example is extracted from:
<?php $imagename = 'test.jpg'; $info = getimagesize($imagename); $fs = filesize($imagename); // Setting the mime-type of the file header("Content-type: {$info['mime']}\n"); // Send as attachment (request client to download) header("Content-Disposition: attachment;". " filename=\"$imagename\"\n"); header("Content-Length: $fs\n"); readfile("$imagename"); ?> Requesting the web client to download a file
References Wiki: List of HTTP headers HTTP/1.1: Header Field Definitions PHP Manual