An updated version of the keystone method. If you are wanting to use it for a doctype other than XHTML 1.1 then a little easy editing is needed. Simon Jessey (another Brit') sent me this updated version which differs to the original in that it works just fine with the W3C Validator.
<?php
/*
This script determines the preferred MIME type of a user agent
and then delivers either application/xhtml+xml or text/html.
Copyright (c) 2003 - 2004 Keystone Websites and Simon Jessey
*/
$charset = "utf-8";
$mime = "text/html";
function fix_code($buffer) {
return (str_replace(" />", ">", $buffer));
}
if(stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml")) {
if(preg_match("/application\/xhtml\+xml;q=([01]|0\.\d{1,3}|1\.0)/i",$_SERVER["HTTP_ACCEPT"],$matches)) {
$xhtml_q = $matches[1];
if(preg_match("/text\/html;q=q=([01]|0\.\d{1,3}|1\.0)/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(stristr($_SERVER["HTTP_USER_AGENT"],"WDG_Validator") ||
stristr($_SERVER["HTTP_USER_AGENT"],"W3C_Validator")) {
$mime = "application/xhtml+xml";
}
if($mime == "application/xhtml+xml") {
$doc_head = "<?xml version=\"1.0\" encoding=\"$charset\"?>\n";
$doc_head = $doc_head."<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"";
$doc_head = $doc_head." \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n";
$doc_head = $doc_head."<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n\n";
$doc_head = $doc_head." <head profile=\"http://gmpg.org/xfn/1\">\n";
} else {
ob_start("fix_code");
$doc_head = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"";
$doc_head = $doc_head." \"http://www.w3.org/TR/html4/loose.dtd\">\n";
$doc_head = $doc_head."<html lang=\"en\">\n\n";
$doc_head = $doc_head." <head profile=\"http://gmpg.org/xfn/1\">\n";
$doc_head = $doc_head." <meta http-equiv=\"content-type\" content=\"$mime;charset=$charset\">\n";
}
header("Content-Type: $mime;charset=$charset");
header("Vary: Accept");
print $doc_head;
?>
Replace you doctype declaration, mime type in the head of your document or template with:
<?php include("pathtowhateveryouhavecalledit.php"); ?>
Many thanks to Simon Jessey for this version.