}
?>
Now that you know how to protect a page, it may be instructive to workthe other
direction, requesting a protected page. As I said earlier, the fopen function allows you to
specify username and password as part of a URL, but you may have a more complicated
situation where you need to use fsockopen. An Authentication request header is
necessary. The value of this header is a username and password separated by a colon.
This string is base64 encoded, in compliance with the HTTP specification.
Listing 18.2 requests the script in Listing 18.1. You may need to mod- ify the URI
to make it work on your Web server. The script assumes you have installed all the
examples on your Web server in /corephp/listings. If you are wondering about
the \r\n at the end of each line, recall that all lines sent to HTTP servers must end in a
carriage return and a linefeed.
Listing 18.2 Requesting a Protected Document
<?
//open socket
if(!($fp = fsockopen("localhost", 80)))
{
print("Couldn't open socket!
\n");
exit;
}
//make request for document
fputs($fp, "HEAD /corephp/listings/18-1.php
HTTP/1.0\r\n");
//send username and password
fputs($fp, "Authorization: Basic ".
base64_encode("leon:secret").
"\r\n");
//end request
fputs($fp, "\r\n");
//dump response from server
fpassthru($fp);
?>
Controlling Browser Cache
One hassle of writing dynamic Web pages is the behavior of caches. Browsers maintain
their own cache, and by default they will check for a newer version of the page only once
per session. Some ISPs provide their own cache as well. The intention is to avoid