Page 220 - Open Soource Technologies 304.indd
P. 220
Web Technologies-I
Notes 9.5.1 Different Content Types
The Content-Type header identifies the type of document being returned. Ordinarily this is
“text/html”, indicating an HTML document. But there are other useful document types, for
example, “text/plain” forces the browser to treat the page as plain text.
9.5.2 Redirections
To send the browser to a new URL, known as a redirection , you set the Location header:
<?php header(‘Location: http://www.example.com/elsewhere.html’); exit( ); ?>
If you provide a partial URL (e.g., “/elsewhere.html”), the redirection is handled internally by
the web server. This is only rarely useful, as the browser generally would not learn that it is not
getting the page it requested. If there are relative URLs in the new document, the browser will
interpret them as being relative to the document it requested, not the document it was sent. In
general, you will want to redirect to an absolute URL.
9.5.3 Expiration
A server can explicitly inform the browser, and any proxy caches that might be between the
server and browser of a specific date and time for the document to expire. Proxy and browser
caches can hold the document until that time or expire it earlier. Repeated reloads of a cached
document do not contact the server. However, an attempt to fetch an expired document does
contact the server.
To set the expiration time of a document, use the Expires header:
header (‘Expires: Fri, 18 Jan 2011 05:30:00 GMT’);
To expire a document three hours from the time the page was generated, use time( ) and
gmstrftime( ) to generate the expiration date string:
$now = time( ); $then = gmstrftime(“%a, %d %b %Y %H:%M:%S GMT”, $now + 60*60*3);
header(“Expires: $then”);
To indicate that a document “never” expires, use the time a year from now:
$now = time( ); $then = gmstrftime(“%a, %d %b %Y %H:%M:%S GMT”, $now + 365*86440);
header(“Expires: $then”);
To mark a document as already expired, use the current time or a time in the past:
$then = gmstrftime(“%a, %d %b %Y %H:%M:%S GMT”); header(“Expires: $then”);
This is the best way to prevent a browser or proxy cache from storing your document:
header(“Expires: Mon, 26 Jul 1910 05:00:00 GMT”); header(“Last-Modified: “ . gmdate(“D, d M Y
H:i:s”) . “ GMT”); header(“Cache-Control: no-store, no-cache, must-revalidate”); header(“Cache-
Control: post-check=0, pre-check=0”, false); header(“Pragma: no-cache”);
9.5.4 Authentication
HTTP authentication works through request headers and response statuses. A browser can send
a username and password (the credentials) in the request headers. If the credentials are not sent
or are not satsifactory, the server sends a “401 Unauthorized” response and identifies the realm
of authentication (a string such as “Mary’s Pictures” or “Your Shopping Cart”) via the WWW-
Authenticate header. This typically pops up an “Enter username and password for ...” dialog box
on the browser, and the page is then re-requested with the updated credentials in the header.
214 LOVELY PROFESSIONAL UNIVERSITY