Anne van Kesteren

Send application/xhtml+xml

 $charset = "utf-8";

if(stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ||
   stristr($_SERVER["HTTP_USER_AGENT"],"Opera/6")           ||
   stristr($_SERVER["HTTP_USER_AGENT"],"Opera/7")           ||
   stristr($_SERVER["HTTP_USER_AGENT"],"WDG_Validator")     ||
   stristr($_SERVER["HTTP_USER_AGENT"],"W3C_Validator")     ||
   stristr($_SERVER["HTTP_USER_AGENT"],"W3C_CSS_Validator")
){
 $mime     = "application/xhtml+xml";
 $advanced = true;
}
else{
 $mime     = "text/html";
 $advanced = false;
}

 header("content-type:$mime;charset=$charset");

I know some people don't like browser sniffing. It is not necessary actually if you don't want to do that. Just remove all the browser specific lines and leave stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml"). That shouldn't be to difficult is it? The method I use is pretty easy to use, if you have PHP installed on your server.

It also sends the character encoding with the header function, so don't need to use a meta element or a processing instruction to do so. The extra variable is there for switching between two methods of stylesheets linking, view my page source in for example Mozilla and IE to see the difference. That's all.

Comments

  1. Maybe I'm just stupid, or something, because I have never been able to figure this out. Where is the PHP script supposed to go to make this work? I tried putting it between the xml prolog and the doctype declaration, but it resulted in a blank page.

    Posted by Simon Jessey at

  2. This should be used before there is any space in the document so right at the top:

    <?php
     /* code starts */
    ?>

    This should be done like this, 'cause of the header function and probably also for the global server variable.

    Posted by Anne at

  3. Okay, I figured out why it is crapping out on me. If I add an XML prolog, it throws up a parsing error. This may be because of the <? delimiter. If I remove the prolog, the page is served correctly. How do you get your XML prolog into your page?

    Posted by Simon Jessey at

  4. My server switched php_short_open_tags off for me. Alternatively, you could print it with PHP:

    print "<?xml version="1.0" encoding="".$encoding.""?>";

    Posted by Anne at

  5. Ahhh. Thanks, Anne. It worked once I had substituted $encoding for $charset.

    (Temporary) test page here: http://jessey.net/tests/

    Posted by Simon Jessey at

  6. A similar script, that I use, can be seen at StijlStek.nl

    Posted by Ben de Groot at

  7. De php code die je geeft is niet helemaal correct voor als er shorttags open staan. Rechtstreeks zo printen print "<?xml version="1.0" encoding="".$encoding.""?>"; zal niet gaan je dient het dan eerst in een variable te zetten dus zo.

    $xmlCode="<?xml version="1.0" encoding="".$encoding.""?>"; print $xmlCode;

    Posted by Folkert at

  8. @Folkert:
    Folkert claims printing the XML prolog string directly is not possible in PHP if shorttags is on. He suggests making a variable for the string first before printing that variable.

    This is not true and hence his solution is not necessary.

    Posted by Martijn at