PHP Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 2.21

Controlling HTTP Response Header Lines

Part:   1  2  3  4  5  6  7 

PHP Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Non ASCII Characters with MySQL

Inputting Non ASCII Characters

Controlling Response Header Lines

HTTP Request Variables

Sessions

Using Cookies

PHP SOAP Extension

PHP SOAP Extension - Server

Directories, Files and Images

Using MySQL with PHP

... Table of Contents

(Continued from previous part...)

Once the Web browser finishes processing the entity body, you can get some limited information from the header lines. For example, you can click the right mouse button and select the properties command on Internet Explorer, it will display some general properties about this response in a pop up window. The properties displayed are not always identical to the response header lines. The "Modified" property is probably identical to the "Last-Modified" entity header line. The "Type" property is sometime related to the "Content-Type" entity header line, and sometimes related to server side resource that generated the response.

How to view all the header lines received in the HTTP response? I couldn't find any existing tools to do this. So I wrote the following program to dump the entire response including all header lines received from a Web server:

<?php # HttpRequestGet.php
# Copyright (c) 2005 by Dr. Herong Yang, http://www.herongyang.com/
# 
   $path = "/index.html";
   $port = 80;
   $host = "localhost";
   if (count($argv) > 1) $path = $argv[1];
   if (count($argv) > 2) $port = $argv[2];
   if (count($argv) > 3) $host = $argv[3];

   $address = gethostbyname($host);
   $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
   socket_connect($socket, $address, $port);
   $in = "GET $path HTTP/1.0\r\n\r\n";
   socket_write($socket, $in, strlen($in));
   while ($out = socket_read($socket, 2048)) {
      echo $out;
   }
   socket_close($socket);
?>

Note that:

  • $argv is built-in array that contains the command line arguments, with $argv[0] representing the PHP file name, if you run your PHP file as "php file_name.php ...".
  • You need to make sure that "extension=php_sockets.dll" is turned on in your php.ini configuration file.

Header Lines of Static Files

Since I am using IIS (Internet Information Service) as the Web server, static files will be served directly by IIS server. It will set the status line and header lines for you based on the information collected from the static files. For example, the "Content-Type" header line will be set based on the file name extension and the MIME settings of the server configuration. The "Content-Length" header line will be set to the size of the file.

Let's look at 3 examples of static files. All of them are stored on my local machine at c:\inetpub\wwwroot, where the IIS is serving documents from.

1. Command: "php HttpRequestGet.php /hello.html" gives us:

HTTP/1.1 200 OK
Server: Microsoft-IIS/5.1
Date: Sun, 13 Nov 2005 04:34:17 GMT
Content-Type: text/html
Accept-Ranges: bytes
Last-Modified: Sun, 13 Nov 2005 04:31:16 GMT
ETag: "608fa7aaf8ebc51:c61"
Content-Length: 38

Hello world!

Couple of interesting notes here:

  • Content-Type was set to "text/html", because the file name extension was "html".
  • The request was marked as HTTP/1.0 in HttpRequestGet, but IIS responded with a higher version, HTTP/1.1.
  • I also tried to use HTTP/1.1 in my request, but IIS returned with an error. Why IIS could not support HTTP/1.1 request?
  • Notice that there was a blank line to separate the header lines and the entity body.
  • Content-

(Continued on next part...)

Part:   1  2  3  4  5  6  7 

Dr. Herong Yang, updated in 2006
PHP Tutorials - Herong's Tutorial Notes - Controlling HTTP Response Header Lines