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

2. Command: "php HttpRequestGet.php /dot.gif" gives us:

HTTP/1.1 200 OK
Server: Microsoft-IIS/5.1
Date: Sun, 13 Nov 2005 04:40:17 GMT
Content-Type: image/gif
Accept-Ranges: bytes
Last-Modified: Sun, 11 Aug 2002 20:48:20 GMT
ETag: "04237ecd343c21:c61"
Content-Length: 43

GIF89a......

As you can see, Content-Type was set correctly to "image/gif" for file name extension "gif", as defined in the MIME settings. I could not include the entity body here because it contains binary data.

3. Command: "php HttpRequestGet.php /hello.pdf" gives us:

HTTP/1.1 200 OK
Server: Microsoft-IIS/5.1
Date: Sun, 13 Nov 2005 04:45:03 GMT
Content-Type: application/pdf
Accept-Ranges: bytes
Last-Modified: Sun, 27 Jul 2003 20:22:12 GMT
ETag: "0b2f4c27c54c31:c61"
Content-Length: 909

%PDF-1.3
% ...
4 0 obj
......

Again, Content-Type was set correctly to "application/pdf" for file name extension "pdf", as defined in the MIME settings. I truncated the entity body to save some space.

Controlling Header Lines - Example Scripts

Now let's see how the PHP engine defines header lines and how you can control them. First create this simple script, hello.php, and copy it to c:\inetpub\wwwroot.

Hello <?php echo "world!"; ?>

Then obtain the response with "php HttpRequestGet.php /hello.php":

HTTP/1.1 200 OK
Server: Microsoft-IIS/5.1
Date: Sat, 19 Nov 2005 02:39:52 GMT
Content-type: text/html
X-Powered-By: PHP/5.0.4

Hello world!

Comparing with the static files, the output shows:

  • PHP engine added its own header line, "X-Powered-By".
  • IIS still keeps the header line, "Server".
  • No more "ETag". Anyway, I don't know what is ETag.
  • PHP defaulted "Content-Type" to "text/html".
  • "Content-Length" is not provided. This is not a good thing, if the client relies on this value.

Now, I am ready to instruct the PHP how to define header lines. Here is my testing script, HttpHeaderLines.php:

<?php #HttpHeaderLines.php
# Copyright (c) 2005 by Dr. Herong Yang, http://www.herongyang.com/
#
   $text = "<html><body>Hello world!</body></html>";
   print($text);
   header("Content-Type: text/xml;charset=utf-8");
   header("Content-Length: ".strlen($text));
   header("Version: Unknown");
   header("Version: 2005");
   header("Key-Word: PHP");
   header("Key-Word: HTTP", false);
?>

(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