/home/wpollock1/public_html/PHP/prolog.php
<?php
/*
One problem with .php files is that the server sets a mime type
based on the file's extension. This must be over-ridden with the
xhtml MIME type. However not all browsers accept xthml (old IE),
so in this case it is better (?) to return an HTML 4.01 MIME type
(even though some of the XHTML page may not render in this case4).
And, if returning HTML then all self-closing tags must be replaced too.
This script from www.workingwith.me.uk/articles/scripting/mimetypes
parses the HTTP_ACCEPT (and if needed, HTTP_USER_AGENT) headers to
see if XHTML is accepted. If so the proper file prolog is generated
(xml, DOCTYPE, and head tags) and the correct MIME content type is
set. Else an HTML prolog and MIME type is generated.
Posted by Neil Crosby on April 17, 2004 08:27 PM
and modified by Wayne Pollock 6/2007 (according to the various
follow-up postings.)
*/
ob_start(); // Output buffering, or can't change headers later.
function unclose_tags ( $buffer ) // When generating HTML (vs. XHTML)
{
return str_replace(" />", ">", $buffer);
}
$mime = "text/html";
$charset = "utf-8";
# special check for the W3C_Validator
if ( isset($_SERVER["HTTP_USER_AGENT"])
&& stristr($_SERVER["HTTP_USER_AGENT"],"W3C_Validator") )
{
$mime = "application/xhtml+xml";
} elseif ( isset($_SERVER["HTTP_ACCEPT"])
&& stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") )
{
if (preg_match("/application\/xhtml\+xml[a-z0-9,\s\/\*\-\+]*"
. ";[\s]?q=([0-1]{0,1}\.\d{0,4})/i",
$_SERVER["HTTP_ACCEPT"],$matches))
{
$xhtml_q = $matches[1];
if ( preg_match("/text\/html[a-z0-9,\s\/\*\-\+]*"
. ";[\s]?q=([0-1]{0,1}\.\d{0,4})/i",
$_SERVER["HTTP_ACCEPT"],$matches) )
{
$html_q = $matches[1];
if ( (float) $xhtml_q >= (float) $html_q )
$mime = "application/xhtml+xml";
}
} else {
$mime = "application/xhtml+xml";
}
}
if ( $mime=="application/xhtml+xml" )
{
$prolog = "<?xml version='1.0' encoding='$charset' ?>\n";
$prolog .= "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 "
. "Strict//EN'\n";
$prolog .= " 'http://www.w3.org/TR/xhtml11/DTD/"
. "xhtml10-strict.dtd'>\n";
$prolog .= "<html xmlns='http://www.w3.org/1999/xhtml' "
. "xml:lang='en' lang='en'>\n";
} else {
ob_start( "unclose_tags" );
$prolog = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN'\n";
$prolog .= " 'http://www.w3.org/TR/html4/strict.dtd'>\n";
$prolog .= "<html lang='en'>\n";
}
# finally, output the mime type and prolog type
header( "Content-Type: $mime;charset=$charset" );
header( "Vary: Accept" );
print $prolog;
?>