CSC 2720 Building Web Applications - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

CSC 2720 Building Web Applications

Description:

What kinds of data are embedded in the HTTP request/response headers? ... What can we achieve by setting HTTP response header? ... Send a different HTTP status code ... – PowerPoint PPT presentation

Number of Views:253
Avg rating:3.0/5.0
Slides: 15
Provided by: cjy6
Category:

less

Transcript and Presenter's Notes

Title: CSC 2720 Building Web Applications


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 223834
GMT Server Apache/1.3.3.7 (Unix)
(Red-Hat/Linux) Last-Modified Wed, 08 Jan 2003
231155 GMT Etag "3f80f-1b6-3e1cb03b" Accept-Ran
ges bytes Content-Length 438 Connection
close Content-Type text/html charsetUTF-8 Body
of the contents goes here
The header section of a HTTP response
  • The data in the HTTP header contains info about
    client/server and the data embedded in the HTTP
    body.

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.s
    erver.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
exit() // Return immediately ?
1 2 3 4 5
When a web client access the above file, it will
go to http//www.yahoo.com immeidately.
  • header() must be called before any actual output
    is sent!

exit() ?
1 2 3 4 5
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
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
no-cache, must-revalidate") ?
1 2 3 4
For the format of HTTP header fields, please
refer to HTTP/1.1 Header Field
Definitions http//www.w3.org/Protocols/rfc2616/r
fc2616-sec14.html The above example is extracted
from http//www.php.net/header
13
Requesting the web client to download a file
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-Dis
position attachment" . "
filename\"imagename\"\n")
header("Content-Length fs\n")
readfile("imagename") ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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
Write a Comment
User Comments (0)
About PowerShow.com