Sending appropriate mimetype and doctype, if supported


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. /* Usage:
  2. If you save this script as 'mimetype.php',
  3. you can include it in a page like this:
  4.  
  5. <?php include ("mimetype.php"); ?>
  6. <title>My Title</title>
  7. </head>
  8. <body>
  9. </body>
  10. </html>
  11.  
  12. As you can see, the <html> and <head> tags have already been 'written' for you.
  13. You can also set $charset to another encoding (like 'utf-8').
  14. */
  15.  
  16. $charset = "iso-8859-1";
  17. $mime = "text/html";
  18.  
  19. function fix_code($buffer) {
  20. return (str_replace(" />", ">", $buffer));
  21. }
  22.  
  23. if(stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml")) {
  24. # if there's a Q value for "application/xhtml+xml" then also
  25. # retrieve the Q value for "text/html"
  26. if(preg_match("/application\/xhtml\+xml;q=0(\.[1-9]+)/i",
  27. $_SERVER["HTTP_ACCEPT"], $matches)) {
  28. $xhtml_q = $matches[1];
  29. if(preg_match("/text\/html;q=0(\.[1-9]+)/i",
  30. $_SERVER["HTTP_ACCEPT"], $matches)) {
  31. $html_q = $matches[1];
  32. # if the Q value for XHTML is greater than or equal to that
  33. # for HTML then use the "application/xhtml+xml" mimetype
  34. if($xhtml_q >= $html_q) {
  35. $mime = "application/xhtml+xml";
  36. }
  37. }
  38. # if there was no Q value, then just use the
  39. # "application/xhtml+xml" mimetype
  40. } else {
  41. $mime = "application/xhtml+xml";
  42. }
  43. }
  44.  
  45. # special check for the W3C_Validator
  46. if (stristr($_SERVER["HTTP_USER_AGENT"],"W3C_Validator")) {
  47. $mime = "application/xhtml+xml";
  48. }
  49.  
  50. # set the prolog_type according to the mime type which was determined
  51. if($mime == "application/xhtml+xml") {
  52. if ( (preg_match("/Gecko|W3C_Validator|MathPlayer/", $_SERVER["HTTP_USER_AGENT"])
  53. && !preg_match("/Chimera|Camino|KHTML/",$_SERVER["HTTP_USER_AGENT"]))
  54. || preg_match("/Camino.*MathML-Enabled/", $_SERVER["HTTP_USER_AGENT"]) ) {
  55. $prolog_type="<?xml version='1.0' encoding='$charset'?>
  56. <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN' 'http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd' >
  57. <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
  58. <head profile='http://gmpg.org/xfn/11'>
  59. <meta http-equiv='Content-Type' content='$mime; $charset' />";
  60. } else {
  61. $prolog_type = "<?xml version='1.0' encoding='$charset' ?>
  62. <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'>
  63. <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
  64. <head profile='http://gmpg.org/xfn/11'>
  65. <meta http-equiv='Content-Type' content='$mime; $charset' />";
  66. }
  67. } else {
  68. ob_start("fix_code");
  69. $prolog_type = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
  70. <html lang='en'>
  71. <head profile='http://gmpg.org/xfn/11'>
  72. <meta http-equiv='Content-Type' content='$mime; charset=$charset' />";
  73. }
  74.  
  75. # finally, output the mime type and prolog type
  76. header("Content-Type: $mime;charset=$charset");
  77. header("Vary: Accept");
  78. print $prolog_type;

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.