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

Here is the output:

HTTP/1.1 200 OK
Server: Microsoft-IIS/5.1
Date: Sat, 19 Nov 2005 02:28:32 GMT
Content-Type: text/xml;charset=utf-8
X-Powered-By: PHP/5.0.4
Content-Length: 38
Version: 2005
Key-Word: PHP
Key-Word: HTTP

<html><body>Hello world!</body></html>

Okay. What can we say about the output?

  • I was able to call header() after I started to output the entity body, because I had the output buffer turned on. I entered "output_buffering = 4096" in php.ini.
  • "Content-Type" defined ok.
  • "Content-Length" defined ok. But it was outputted after "X-Powered-By". Not sure why.
  • The "replace" flag worked. There is only one "Version", because by default replace is turned on. There are two "Key-Word", because I set "replace" to "false" in my second header() call.

Forcing the Browser to Redirect

The document says we can use header("Location: ...") to tell the browser to make a new HTTP request to a given URL. This is called "redirect". Here is my testing script, HttpRedirect.php:

<?php #HttpRedirect.php
# Copyright (c) 2005 by Dr. Herong Yang, http://www.herongyang.com/
#
   $text = "<html><body>Moved!</body></html>";
   print($text);
   header("Content-Type: text/xml;charset=utf-8");
   header("Content-Length: ".strlen($text));
   header("Location: http://www.herongyang.com/");
?>

The output:

HTTP/1.1 302 Object Moved
Server: Microsoft-IIS/5.1
Date: Sat, 19 Nov 2005 03:46:58 GMT
Content-Type: text/html
X-Powered-By: PHP/5.0.4
Content-Length: 32
Location: http://www.herongyang.com/

<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be
 found <a HREF="http://www.herongyang.com/">here</a></body>

I am a little bit surprised by the output:

  • My entity body has been totally replaced by a short HTML document generated by the PHP engine. This is not mentioned in the PHP documentation at all!
  • The status line did change to "HTTP/1.1 302 Object Moved".
  • The "Content-Length" still had my value, the number of bytes of my original HTML message, not the replacing message.

(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