Return to Snippet

Revision: 790
at August 8, 2006 13:05 by rolandog


Updated Code
/*	Usage:
If you save this script as 'mimetype.php',
you can include it in a page like this:

	<?php include ("mimetype.php"); ?>
	<title>My Title</title>
	</head>
	<body>
	</body>
	</html>

As you can see, the <html> and <head> tags have already been 'written' for you.
You can also set $charset to another encoding (like 'utf-8').
*/

$charset = "iso-8859-1";
$mime = "text/html";

function fix_code($buffer) {
   return (str_replace(" />", ">", $buffer));
}

if(stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml")) {
   # if there's a Q value for "application/xhtml+xml" then also 
   # retrieve the Q value for "text/html"
   if(preg_match("/application\/xhtml\+xml;q=0(\.[1-9]+)/i",
                 $_SERVER["HTTP_ACCEPT"], $matches)) {
      $xhtml_q = $matches[1];
      if(preg_match("/text\/html;q=0(\.[1-9]+)/i",
                    $_SERVER["HTTP_ACCEPT"], $matches)) {
         $html_q = $matches[1];
         # if the Q value for XHTML is greater than or equal to that 
         # for HTML then use the "application/xhtml+xml" mimetype
         if($xhtml_q >= $html_q) {
            $mime = "application/xhtml+xml";
         }
      }
   # if there was no Q value, then just use the 
   # "application/xhtml+xml" mimetype
   } else {
      $mime = "application/xhtml+xml";
   }
}

# special check for the W3C_Validator
if (stristr($_SERVER["HTTP_USER_AGENT"],"W3C_Validator")) {
   $mime = "application/xhtml+xml";
}

# set the prolog_type according to the mime type which was determined
if($mime == "application/xhtml+xml") {
   if ( (preg_match("/Gecko|W3C_Validator|MathPlayer/", $_SERVER["HTTP_USER_AGENT"])
      && !preg_match("/Chimera|Camino|KHTML/",$_SERVER["HTTP_USER_AGENT"]))
      || preg_match("/Camino.*MathML-Enabled/", $_SERVER["HTTP_USER_AGENT"]) ) {
      $prolog_type="<?xml version='1.0' encoding='$charset'?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN' 'http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd' >
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
<head profile='http://gmpg.org/xfn/11'>
<meta http-equiv='Content-Type' content='$mime; $charset' />";
   } else {
      $prolog_type = "<?xml version='1.0' encoding='$charset' ?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
<head profile='http://gmpg.org/xfn/11'>
<meta http-equiv='Content-Type' content='$mime; $charset' />";
   }
} else {
   ob_start("fix_code");
   $prolog_type = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
<html lang='en'>
<head profile='http://gmpg.org/xfn/11'>
<meta http-equiv='Content-Type' content='$mime; charset=$charset' />";
}

# finally, output the mime type and prolog type
header("Content-Type: $mime;charset=$charset");
header("Vary: Accept");
print $prolog_type;

Revision: 789
at August 8, 2006 13:04 by rolandog


Initial Code
/*	Usage:
If you save this script as 'mimetype.php',
you can include it in a page like this:

	<?php include ("mimetype.php"); ?>
	<title>My Title</title>
	</head>
	<body>
	</body>
	</html>

As you can see, the <html> and <head> tags have already been 'written' for you.
You can also set $charset to another encoding (like 'utf-8').
*/
<?php
$charset = "iso-8859-1";
$mime = "text/html";

function fix_code($buffer) {
   return (str_replace(" />", ">", $buffer));
}

if(stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml")) {
   # if there's a Q value for "application/xhtml+xml" then also 
   # retrieve the Q value for "text/html"
   if(preg_match("/application\/xhtml\+xml;q=0(\.[1-9]+)/i",
                 $_SERVER["HTTP_ACCEPT"], $matches)) {
      $xhtml_q = $matches[1];
      if(preg_match("/text\/html;q=0(\.[1-9]+)/i",
                    $_SERVER["HTTP_ACCEPT"], $matches)) {
         $html_q = $matches[1];
         # if the Q value for XHTML is greater than or equal to that 
         # for HTML then use the "application/xhtml+xml" mimetype
         if($xhtml_q >= $html_q) {
            $mime = "application/xhtml+xml";
         }
      }
   # if there was no Q value, then just use the 
   # "application/xhtml+xml" mimetype
   } else {
      $mime = "application/xhtml+xml";
   }
}

# special check for the W3C_Validator
if (stristr($_SERVER["HTTP_USER_AGENT"],"W3C_Validator")) {
   $mime = "application/xhtml+xml";
}

# set the prolog_type according to the mime type which was determined
if($mime == "application/xhtml+xml") {
   if ( (preg_match("/Gecko|W3C_Validator|MathPlayer/", $_SERVER["HTTP_USER_AGENT"])
      && !preg_match("/Chimera|Camino|KHTML/",$_SERVER["HTTP_USER_AGENT"]))
      || preg_match("/Camino.*MathML-Enabled/", $_SERVER["HTTP_USER_AGENT"]) ) {
      $prolog_type="<?xml version='1.0' encoding='$charset'?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN' 'http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd' >
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
<head profile='http://gmpg.org/xfn/11'>
<meta http-equiv='Content-Type' content='$mime; $charset' />";
   } else {
      $prolog_type = "<?xml version='1.0' encoding='$charset' ?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
<head profile='http://gmpg.org/xfn/11'>
<meta http-equiv='Content-Type' content='$mime; $charset' />";
   }
} else {
   ob_start("fix_code");
   $prolog_type = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
<html lang='en'>
<head profile='http://gmpg.org/xfn/11'>
<meta http-equiv='Content-Type' content='$mime; charset=$charset' />";
}

# finally, output the mime type and prolog type
header("Content-Type: $mime;charset=$charset");
header("Vary: Accept");
print $prolog_type;
?>

Initial URL


Initial Description
Sends as XHTML 1.1 + MathML if supported, falls back to XHTML 1.1 if supported, or falls back to HTML 4.01 Strict.

Mixed two separate scripts:
http://www.workingwith.me.uk/articles/scripting/mimetypes
http://golem.ph.utexas.edu/~distler/blog/archives/000367.html

Initial Title
Sending appropriate mimetype and doctype, if supported

Initial Tags
php, html, xhtml

Initial Language
PHP