Forum Moderators: coopster

Message Too Old, No Replies

Changing the HTTP Response Header

HTTP Response Header

         

shaowei

4:03 am on Feb 16, 2005 (gmt 0)

10+ Year Member



I would like to know if there is any way in which I can change my HTTP response header.

There's a site called web-sniffer.net that can get your http response header.

I notice a difference in the HTTP response header between a php site and a static html site.

PHP files has HTTP response header of
Date, Server, X-Powered-By, Set-Cookie, Expires, Cache-Control, Pragma, Connection, Transfer-Encoding and Content-Type.

HTML files has HTTP response header of
Date, Server, Last-Modified, ETag, Accept-Ranges, Content-Length, Connection and Content-Type.

I'm currently using forcetype on my server in such a way that my all my php file are saved in html filetype. However, the HTTP response header returned by these html files are still like those of the php files.

Is there anyway that I could change the HTTP response header of my "supposingly" html files to look like those of php files.

grandpa

4:20 am on Feb 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There is a way. I've placed this code in an include file that is included in most of my php pages. Let this serve an an example, and modify to suit your own needs. My goal was to have the php page headers look more like a standard html header.

<?php
global $HTTP_IF_MODIFIED_SINCE;
$if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE);
$mtime = filemtime($filename);
$etag = '"'.md5($mtime).'"';
$gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
if ($if_modified_since == $gmdate_mod) {
header("HTTP/1.0 304 Not Modified");
}
else {
header("Connection: Keep-Alive");
header("ETag: $etag");
header("Last-Modified: $gmdate_mod");
header("Keep-Alive: timeout=5, max=100");
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
header('Pragma: no-cache');
}
?>

<aside> Posting the same question in different forums will generate more flax than response, and it is bad form.
You'll soon discover that WW is a most helpful place (as long you you keep rabbit recipies out of Foo.) </aside>

And, in case no else has mentioned it yet, Welcome to WebmasterWorld.

shaowei

4:45 am on Feb 16, 2005 (gmt 0)

10+ Year Member



Thanz grandpa.

It works! However, is that any way that i can forbid some response header to be shown. For example, with your help, now i can add in any response header that I want. However, I could not get rid of headers like X-Powered-By, Cache-Control that I would not want.

Is there anyway to get rid of such headers?

grandpa

5:00 am on Feb 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hmmm, I haven't had the pleasure of trying to suppress specific headers. But if I wanted to, I would start looking at the header function [us3.php.net] documentation.

Let us know how it turns out.

thijsnetwork

2:12 pm on Feb 16, 2005 (gmt 0)

10+ Year Member



You can switch of the X-Powered-By header by changing your php.ini configuration file.

Add this line:

expose_php = off