1 / 14

CSC 2720 Building Web Applications

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?

Download Presentation

CSC 2720 Building Web Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 2720Building Web Applications Getting and Setting HTTP Headers (With PHP Examples)

  2. 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

  3. Introduction HTTP/1.1 200 OK Date: Mon, 23 May 2005 22:38:34 GMT Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux) Last-Modified: Wed, 08 Jan 2003 23: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 data in the HTTP header contains info about client/server and the data embedded in the HTTP body. The header section of a HTTP response

  4. 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

  5. Obtaining HTTP Header Fields • Header fields of the current request is stored in $_SERVER. • For a complete list of predefined names, please refer to http://www.php.net/manual/en/reserved.variables.server.php • 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.

  6. 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.)

  7. 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

  8. 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.

  9. 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

  10. Redirecting the web client to another URL • header() must be called before any actual output is sent! 1 2 3 4 5 <?php header('Location: http://www.yahoo.com/'); exit(); // Return immediately ?> When a web client access the above file, it will go to http://www.yahoo.comimmeidately. 1 2 3 4 5 <?php header('Location: http://www.yahoo.com/'); exit(); ?> Even with only one empty space sent, subsequent calls to header() will fail.

  11. Send a different HTTP status code 1 2 3 4 5 6 <?php header("HTTP/1.0 404 Not Found"); ?> <html> <!-- Content of the error page goes here … --> </html> 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.

  12. Tell the web client whether to cache the current document or not 1 2 3 4 <?php // HTTP/1.1 header("Cache-Control: no-cache, must-revalidate"); ?> • For the format of HTTP header fields, please refer to • HTTP/1.1: Header Field Definitions • http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html • The above example is extracted from: • http://www.php.net/header

  13. Requesting the web client to download a file 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <?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"); ?>

  14. References • Wiki: List of HTTP headers • http://en.wikipedia.org/wiki/List_of_HTTP_headers • HTTP/1.1: Header Field Definitions • http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html • PHP Manual • http://www.php.net/manual/en/index.php

More Related