How to set Mimetype application/xhtml+xml headers in PHP
The basic idea is pretty simple. Create this function, call it the first line of every page. I found this on someone's site, I made a small change, here is the original source:
:: Code :: function set_header()
{ $charset = "iso-8859-1"; $mime = "text/html"; 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"; } } // output the mime type and prolog type header("Content-Type: $mime;charset=$charset"); header("Vary: Accept"); } Basically what happens is that when you run this first on the page, like this: :: Code :: <?php
include('siteprogramming.php');// contains the above function set_header(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head>.... etc It will query the browser, and if the browser supports xhtml, it will return the XHTML page as mimetype xhtml, with varying results, your javascript will be toast probably, except on Opera 7.5, which violates the standards in order to run things like document.write. On the bright side, it's an easy way to error check your XHTML pages, since you can browse through your site and any page with errors will have a parse error, page does not load, just an error screen with the error line number. The Tests We made 3 sample pages to demonstrate what happens with application/xhtml+xml mimetypes, and one with straight application/xml mimetype:
These are the results we saw: We tested this with IE, Mozilla, Opera, and Konqueror 3.2.2, which should work fairly much like Safari. Try it yourselves and see. Opera returns this parse error for the invalid XHTML page: :: Code :: XML parsing failed: not well-formed (Line: 41, Character: 21)Mozilla returns this one: :: Code :: XML Parsing Error: not well-formed Location: http://techpatterns.com/downloads/examples/xhtml_mime_error.php Line Number 44, Column 22:With application/xhtml+xml Internet Explorer 5x will ask what to do with the unknown filetype , then fail to load, IE 6 likewise does the same, in other words, IE does not support application/xhtml+xml at all. With application/xml Mozilla and Opera display the page the same as with xhtml+xml, and IE 6 displays the xml unparsed, as you can see by viewing the various pages. Konqueror / Safari do not appear to support application/xhtml+xml at all, but did display the application/xml page like IE 6 did, straight unparsed xml, after a popup box asked me to select the default viewer for xml types. Real world XHTML useage There's an interesting article about how many supposed XHTML pages actually were valid XHTML. :: Quote :: Of the 119 XHTML sites tested:
* 88 sites (74%) failed Test 1 ("Simple Site Validation"). * 18 sites (15%) passed Test 1, but failed Test 2 ("The Laziness Test") [only valid home or index page]. * 12 sites (10%) passed Test 2, but failed Test 3 ("The MIME-type Test"). * Leaving us with one site (1%) that managed to pass all three tests. Although the author does not include this question: :: Quote :: The "fourth" test described in the earlier entry gets at the question of, "Why are you using XHTML in the first place?" it's actually a very good one. Why are you trying to use XHTML? Are you following a fad? Are you doing it because everyone says you should? Do these people have any idea what they're talking about? I've seen this question discussed ad nauseum on forums, but very few posters seem able to think clearly on this question, although there are some very pleasant exceptions to that rule. Most other posters do not do nearly as well, tending to fall into the 'use it because you should, or it's the latest thing, or it's cool, or whatever, lines of reasoning. We use it so we can learn these kinds of problems, although I'd have to admit that I first used it because I thought it was somehow 'better' or 'cooler', as if HTML can be 'cool'. Now we use it for testing purposes primarily. By the way, we fail that test too, on number 3, for the simple reason that we don't feel like reprogramming the site javascript at the moment, which uses quite a bit of javascript document.write. Back to top |
|||||
XHTML compliant Javascript
As was mentioned, XHTML mimetypes do not allow the javascript document.write method. If you really need to run your site as XHTML 1 and you were using document.write, you'll need to use a method like this, originally posted on expertsexchange.. Slightly clarifying explanation is here, but no solution is offered.
You can read more discussion on Mozilla's bugzilla. This is pretty interesting reading, it shows why all browsers have differences, they have to interpret standards, and make practical useability decisions, Opera for example decided to support document.write, which I think was the right decision because who on earth wants to create some convoluted javascript just to fit some arcane and unrealistic 'standard' put forth for some strange, incomprehensible reason? Back to top |
|||||
I have a question with regards to IE. What to do if mime application/xhtml+xml is not recognized? The page loads fine in Firefox and Safari, and validates successfully as being XHTML 1.0 Strict. However under IE, trying to open the page results in a file download dialogue.
--- [new user link] Back to top |
|||||
The real answer is to stop using that idiotic xhtml garbage and move to html 5.
I do real xhtml sites for testing and I promise you it's a total and utter joke, that benefits nobody. In answer, you serve it as mimetype text/html for MSIE. Not sure about msie 9, but really who cares? You can't run or maintain real xhtml because a single parsing error should cause a page load error and failure to parse. XHTML was the stupidest idea ever to hit the world of the internet and thank god with html 5 we can finally move in a positive productive direction again, at least we can once all the stupid script kiddies who blindly repeated to use xhtml because it was new are re-educated and learn to use html 5.... that will take a while to weed them out of all the projects out there though. Back to top |
|||||
Split offtopic xhtml talk to the HTML forums. If someone cares about this question, follow that discussion there.
Back to top |
|||||
All times are GMT - 8 Hours
|