Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Serving XHTML in PHP


  • Please log in to reply
3 replies to this topic

#1 phillw


    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts

Posted 19 September 2009 - 01:49 PM


I've been having some problems with the standard XHTML header on my php pages...

I dug this code out from the net - Is it still considered the 'best' way to handle the issue, or does anyone know of newer code ?

$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"
                 $_SERVER["HTTP_ACCEPT"], $matches)) {
      $xhtml_q = $matches[1];
                    $_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") {
   $prolog_type = "<?xml version='1.0' encoding='$charset' ?>
      <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.1//EN' 
      <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>";
} else {
   $prolog_type = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 
      <html lang='en'>";

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

I understand that there are issues with firefox / mozilla needing some extrs commands for working out the Q value, but this is all very new to me !! - My 'normal' coding passes the xhtml strict validation no problems. So, I'd really like to be able to ensure that my php code also delivers XHTML compliant headers.

Many thanks,


Since posting this, I have found this site - has anyone had any dealings with either sets of code ?

PHP, XHTML MIME type and Caching - Frames of Reference

Edited by phillw, 19 September 2009 - 02:19 PM.
different site

  • 0

#2 John


    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 20 September 2009 - 09:22 AM

Is there a reason you can't just hard code the headers? From my experience, thats what most people do.
  • 0

#3 phillw


    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts

Posted 20 September 2009 - 12:16 PM

Is there a reason you can't just hard code the headers? From my experience, thats what most people do.

The module dissects to see if xhtml is available - evidently not in IE6 and was, at the time of writing of the module not in IE7 beta either. As I use F/fox - it's not a problem for me, just a case of being 'kind' to 'crippled' browsers. Serving up the correct MIME type. This standards bit is yet another new area for me. Whilst I don't have the highest regard for IE, I have to accept that most people use it & that there are the older versions out there, which I guess, I should 'look after'.

An example is the use of collapse-all in the CSS for tables when it detects IE6 - a simple bit of code, that uses minimal extra cpu time - but renders tables correctly in IE6.

I don't know what %age of the population still have such 'crippled' browsers - but, from what I read so far - we are supposed to be 'gentle' with them :sneaky:

The idea of NOT hard coding the header on each page, is that I don't have the code on every page - just as an include - much like one uses an include 'dbconnect' to connect to your db - instead of coding it into every page that needs access to the db !!

If you think that the vast majority of browsers currently in ACTIVE use are xhtml compliant - then I can stick to just that mime type - I dunno, it's not to much of an overhead to check.

When I decided to use XHTML 'Strict' - I must have had a few glasses of wine .... Strict ???? More like a traffic warden having a 'bad' day at home check to see if they can issue you a ticket !!!!

Between the two excellent articles on here about using MySQL blobs for storing images and retrieving them & this

Docs/BasicUsage ? PHP Thumbnailer

I'm going to be handing virtually everything to php & MySQL . You'll all be invited to comment on the maths of the site (learning fulltext boolean searches, adding in sub - searches of the likes of 1/4" and 1mm have been a good learning curve :cursing: )

But, I digress, should I bother checking for the browsers who can't handle xhtml, or is it still considered 'best practice' to allow for them and serve it up as html in the mime type?


  • 0

#4 zeroradius


    Speaks fluent binary

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1085 posts

Posted 30 December 2010 - 08:14 AM

@raven - create your own thread for your problem. (post moved to own thread [mod comment])

@phillw - Well according to wikipedia even IE8 doesn't have true XHTML support but it is supose to be fixed by IE9. What are you planning to do with the XHTML thou? Most people at present time just use XHTML to require the code to be properly formated because there are a few browsers that don't support XHTML but if your only using it for formatting it will render as HTML in the non supported browsers.

Edited by Orjan, 30 December 2010 - 10:37 AM.

  • 0
Posted Image

Also tagged with one or more of these keywords: xhtml

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download