[thelist] Set MIME type via PHP (thoughts please)

Timothy J. Luoma luomat at operamail.com
Sat Sep 6 15:13:36 CDT 2003


I have a little PHP snippet like so:

<?php

$ACCEPT       = $_SERVER["HTTP_ACCEPT"];

if (strstr($ACCEPT,'application/xhtml')) {
	header("Content-type: application/xhtml+xml");
}

?>

which I thought was pretty clever.  Basically if the browser says it can 
handle application/xhtml then give it.

Note that this is not intended to be the most robust solution in the 
world.  It doesn't check to see if there is a higher level of preference 
for one of the other XHTML MIME types.  Frankly that doesn't bother me.

However, someone pointed out that it would be possible (theoretically at 
least) to have a browser which sends a HTTP_ACCEPT which has this:

	 application/xhtml+xml;q=0.0

which basically means "Don't send me application/xhtml+xml" and my script 
wouldn't handle that.

This also doesn't bother me.  It seems dumb to configure your browser to 
say what it doesn't want, and the HTTP_ACCEPT has *ACCEPT* right in the 
name... it's not HTTP_REJECT.

But I digress.

I was hoping that someone smarter than myself would be able to take this 
humble beginning and make it a bit more robust.  So I offer it here.

The original script looked like this, back when I had a static IP:

<?php

  $REMOTE_HOST     = $_SERVER["REMOTE_ADDR"];

  $FOOTER_MSG	= "" ;

  if (
  	("$REMOTE_HOST" == '10.10.10.10') // where 10.10.10.10 is my static IP
     )
  {
  	$ACCEPT       = $_SERVER["HTTP_ACCEPT"];

  	if (strstr($ACCEPT,'application/xhtml')) {
  		header("Content-type: application/xhtml+xml");
  	}
  }

?>

That would serve application/xhtml only if the request came from me.

Anyway, for what it's worth.

TjL

ps -- you can check your HTTP_ACCEPT and other headers at 
http://www.tntluoma.com/php/ua.php  It has come in handy for me a few 
times when using different browsers.





More information about the thelist mailing list