sending the w3c validator different mime types

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
octavian_3009
Forum Newbie
Posts: 9
Joined: Tue Feb 24, 2009 6:28 pm

sending the w3c validator different mime types

Post by octavian_3009 »

Hello,

I am attempting to generate code which will allow users to check my pages with the w3c validator. Right now, I have the following code:

Code: Select all

<p><a href="http://validator.w3.org/check?uri=<?php $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
 
echo $url; ?>">
<?php if (!empty($_SERVER["QUERY_STRING"]))
    $url .= "?".$_SERVER['QUERY_STRING'];
    
    if($mime == "application/xhtml+xml") {
   $validate_type = "<object width=\"88px\" height=\"31px\" type=\"image/png\" data=\"http://www.w3.org/Icons/valid-xhtml11.png\">Valid xhtml 1.1</object>";
} else {
   $validate_type = "<object width=\"88px\" height=\"31px\" type=\"image/png\" data=\"http://www.w3.org/Icons/valid-html401.png\">Valid html 4.01</object>";
}
print $validate_type; ?></a></p>

While the above code does generate a link which asks the validator to check the page, the validator always checks the xhtml 1.1 version rather than the html 4.01. I've configured my pages to be sent as application/xhtml+xml to user agents which accept that mime type and text/html to the others. At the footer of my pages, the Image image appears when visitors view the site through an application/xhtml+xml capable browser (such as Firefox), but page viewers seeing the site through a browser which does not understand real xhtml (like Internet Explorer), users see the image Image instead.

Here is what I want to be able to have:

1. Code which can tell the validator to check the referring page
The above code does meet this need

2. Code which displays Image to users receiving the text/html (and html 4.01) version of the page and which displays Image to users receiving the application/xhtml+xml (with an xhtml 1.1 configuration).
The above code does meet this need

3. Code which causes the validator to receive the text/html version when the Image image is clicked and causes the validator to receive the application/xhtml+xml version when the Image image is clicked.
The above code does not meet this need

Right now, the validator always receives the xhtml version and validates it in that way. It would be confusing for someone to click Image and, on the next page, read "This document was successfully checked as XHTML 1.1" While this issue may not matter to some, it is one which I would like to resolve if possible.


Is there a way to send the validator same mime type (and code) that the current user has received?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: sending the w3c validator different mime types

Post by requinix »

You can't serve XHTML 1 and HTML 4 at the same time - the <!DOCTYPEs are different.
It is those that the validator looks at to determine what it should validate as.
octavian_3009
Forum Newbie
Posts: 9
Joined: Tue Feb 24, 2009 6:28 pm

Re: sending the w3c validator different mime types

Post by octavian_3009 »

I'm not trying to send both doctypes at the same time. What I want is for users viewing the page in browsers like Internet Explorer to see the html 4 validation check and visitors using Firefox (or a similar broswer) to see the result of the validator's check of the xhtml 1 version. It would not check both doctypes at the same time. It would check whichever version the visitor's browser has received.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: sending the w3c validator different mime types

Post by requinix »

...which is going to be the exact same thing if all you do is change the MIME type you send the browser.

If the page has the doctype for XHTML 1 then it will not be valid HTML 4, and regardless of whether it's text/html or application/xhtml+xml it's still going to be rendered the same way (in Firefox... IE will do a download prompt for the latter).
octavian_3009
Forum Newbie
Posts: 9
Joined: Tue Feb 24, 2009 6:28 pm

Re: sending the w3c validator different mime types

Post by octavian_3009 »

http://www.workingwith.me.uk/articles/s ... /mimetypes My configuration is based (with a few modifications) on this article. When a browser requests one of my pages and says it understands real xhtml, that is what the browser will receive. Otherwise, the browser will receive a valid html 4 version of the page. Is there a way to send the validator same mime type (and code) that the user's browser receives?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: sending the w3c validator different mime types

Post by requinix »

Mmm... well, my feelings on the matter aren't important...

The browser would have to upload its version of the page to the validator and that doesn't normally happen.

You could add something to the URL to validate indicating what type of page should be served to the validator. Like add a "validate=xhtml" or "validate=html" to the $url, then make your script decide which format to send based on $_GET["validate"] (if not present then it uses the Accept header like normal).
octavian_3009
Forum Newbie
Posts: 9
Joined: Tue Feb 24, 2009 6:28 pm

Re: sending the w3c validator different mime types

Post by octavian_3009 »

Mmm... well, my feelings on the matter aren't important...
Did I misinterpret or fail to address something you said? If so, that was not my intention. When I said that browsers would receive the xhtml version if they could understand it, I meant that I had set my script to check for, and send it that way if possible.

Like add a "validate=xhtml" or "validate=html" to the $url, then make your script decide which format to send based on $_GET["validate"]
This makes some sense, but I don't understand all of what you are saying. Is there an example you might be able to give?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: sending the w3c validator different mime types

Post by requinix »

I was going to say something about how I don't like this html/xhtml switching... well, rant, really... but that wouldn't help anybody.

Code: Select all

<p><a href="http://validator.w3.org/check?uri=<?php $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
 
echo $url; ?>">
I'm saying, change the $url so that it includes some stuff that lets the script know it should serve a specific type of page. Normally you'd be using the $_SERVER["HTTP_ACCEPT"] to determine which you should serve: text/html or application/xhtml+xml. Before looking at that, check for (whatever you put into the URL).

Example:

Code: Select all

<p><a href="http://validator.w3.org/check?uri=<?php $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']; 
 
// the & is encoded to %26 so your script gets it and not the validator
if (serving an html page) {
    $url .= "%26forcetype_html=";
} else { // serving an xhtml page
    $url .= "%26forcetype_xhtml=";
}
 
echo $url; ?>">
And when you determine whether to serve HTML or XHTML,

Code: Select all

if (isset($_GET["forcetype_html"])) {
    serve an html page;
} else if (isset($_GET["forcetype_xhtml"])) {
    serve an xhtml page;
} else {
    use $_SERVER["HTTP_ACCEPT"] to decide which type to serve;
}
octavian_3009
Forum Newbie
Posts: 9
Joined: Tue Feb 24, 2009 6:28 pm

Re: sending the w3c validator different mime types

Post by octavian_3009 »

// the & is encoded to %26 so your script gets it and not the validator
Which "&" are you referring to and how is it encoded to %26? The example you gave is appreciated does make more sense, but I don't understand how the page URL actually gets changed. If a user was viewing <http://www.example.org/example_page.html> as xhtml, would it look like <http://www.example.org/example_page.htm ... ype_xhtml=> and <http://www.example.org/example_page.htm ... _html=>for html?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: sending the w3c validator different mime types

Post by requinix »

octavian_3009 wrote:
// the & is encoded to %26 so your script gets it and not the validator
Which "&" are you referring to and how is it encoded to %26? The example you gave is appreciated does make more sense, but I don't understand how the page URL actually gets changed. If a user was viewing <http://www.example.org/example_page.html> as xhtml, would it look like <http://www.example.org/example_page.html%26forcetype_xhtml=> and <http://www.example.org/example_page.html%26forcetype_html=>for html?
Actually no. The link to the validator would look like

Code: Select all

http://validator.w3.org/check?uri=http://www.example.org/example_page.html%3fforcetype_xhtml=
Their server will then translate the URL to validate into

Code: Select all

http://www.example.org/example_page.html?forcetype_xhtml=
%26 represents a & and %3f represents a ?. You'd be using whichever was appropriate (the former if there is a $_SERVER["QUERY_STRING"] and the latter if there was not).
octavian_3009
Forum Newbie
Posts: 9
Joined: Tue Feb 24, 2009 6:28 pm

Re: sending the w3c validator different mime types

Post by octavian_3009 »

Here is code that I now have thanks to your suggestions.

Code: Select all

<p><a href="http://validator.w3.org/check?uri=<?php $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
     
    // the & is encoded to %26 so your script gets it and not the validator
    if($mime = "application/xhtml+xml") {
        $url .= "%26forcetype_xhtml=";
    } else { // serving an xhtml page
        $url .= "%26forcetype_html=";
    }
     
   echo $url; ?>">
  <?php if (!empty($_SERVER["QUERY_STRING"]))
    $url .= "?".$_SERVER['QUERY_STRING'];
    
    if($mime == "application/xhtml+xml") {
   $validate_type = "<object width=\"88px\" height=\"31px\" type=\"image/png\" data=\"http://www.w3.org/Icons/valid-xhtml11.png\">Valid xhtml 1.1</object>";
} else {
   $validate_type = "<object width=\"88px\" height=\"31px\" type=\"image/png\" data=\"http://www.w3.org/Icons/valid-html401.png\">Valid html 4.01</object>";
}
print $validate_type; ?></a></p>
 
While this code does show the link properly in the footer of my pages, the server stills does not seem to know what to do with these modified urls. Here is the message that the validator gives me when I try to validate the xhtml version of the page. http://validator.w3.org/check?uri=http: ... ype_xhtml= This is very close to working, but the server stills needs to be able to make sense of the url request it receives. Are there any problems that you can see with the above (in this post) code?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: sending the w3c validator different mime types

Post by requinix »

If you see the URL that the validator complains about it says "...schedule.html&forcetype_xhtml=". That & should be a ?.
But if there was a query string to the script then it should be a &. In your case there won't be one since you're using SCRIPT_NAME.

Code: Select all

    // the ? is encoded to %3f so your script gets it and not the validator
    if($mime = "application/xhtml+xml") {
        $url .= "%3fforcetype_xhtml=";
    } else { // serving an xhtml page
        $url .= "%3fforcetype_html=";
    }
The XHTML version works but the HTML one is still validating as XHTML 1.1.
You still need to add the code to your site that uses the URL to switch between X/HTML.
octavian_3009
Forum Newbie
Posts: 9
Joined: Tue Feb 24, 2009 6:28 pm

Re: sending the w3c validator different mime types

Post by octavian_3009 »

@tasairis

Thanks to your help, I have managed to find a solution to the problem. While trying to get the server to send the text/html version of my pages when receiving "forcetype_html=," I noticed that the validator was giving some strange URL's like <http://validator.w3.org/check?uri=http: ... _Validator[/b]%2F1.606>. After doing some experimentation, I remeber that one of my php scripts (from http://www.workingwith.me.uk/articles/s ... /mimetypes )was sending mimetypes based on browser names and types. It took some time, but here is the code that I finally ended up with:

Code: Select all

<?php
if (stristr($_SERVER["HTTP_USER_AGENT"],"Mozilla")) {
   $mime = "application/xhtml+xml";
}
 
if (stristr($_SERVER["HTTP_USER_AGENT"],"IE")) {
   $mime = "text/html";
}
 
# special check for the W3C_Validator
if (stristr($_SERVER["HTTP_USER_AGENT"],"W3C_Validator")) {
   $mime = "application/xhtml+xml";
}
?>
<p><a href="http://validator.w3.org/check?uri=<?php $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
     
   echo $url; ?>&charset=(detect+automatically)&doctype=Inlineamp;&group=0&user-agent=Mozilla%2F1.606"><object width="88px" height="31px" type="image/png" data="http://www.w3.org/Icons/valid-xhtml11.png">Valid xhtml 1.1</object></a>
<a href="http://jigsaw.w3.org/css-validator/validator?uri=<?php $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
     echo $url; ?>"><object width="88px" height="31px" type="image/png" data="http://www.w3.org/Icons/valid-css">Valid css</object></a>
</p>

I'll explain what I did in a bit more detail to help others who may benefit from this thread in the future. First, I gave instructions as to which browsers should receive which mimetype:

Code: Select all

<?php
if (stristr($_SERVER["HTTP_USER_AGENT"],"Mozilla")) {
   $mime = "application/xhtml+xml";
}
 
if (stristr($_SERVER["HTTP_USER_AGENT"],"IE")) {
   $mime = "text/html";
}
 
# special check for the W3C_Validator
if (stristr($_SERVER["HTTP_USER_AGENT"],"W3C_Validator")) {
   $mime = "application/xhtml+xml";
}
?>
Because Mozilla style browsers can usually (but not always) render pages as application/xhtml+xml, browsers claiming to be like Mozilla receive that mimetype. Internet Explorer type browsers on the other hand, receive the text/html version of the page.

Code: Select all

<p><a href="http://validator.w3.org/check?uri=<?php $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
     
   echo $url; ?>&charset=(detect+automatically)&doctype=Inlineamp;&group=0&user-agent=Mozilla%2F1.606"><object width="88px" height="31px" type="image/png" data="http://www.w3.org/Icons/valid-xhtml11.png">Valid xhtml 1.1</object></a>
<a href="http://jigsaw.w3.org/css-validator/validator?uri=<?php $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
     echo $url; ?>"><object width="88px" height="31px" type="image/png" data="http://www.w3.org/Icons/valid-css">Valid css</object></a>
</p>
This code tells the php script to have the validator check the Mozilla (application/xhtml+xml) version when viewing the page as xhtml 1.1. Using some of the "find and replace" functionality of php (like ob_start mentioned in http://www.workingwith.me.uk/articles/s ... /mimetypes ) the "user-agent=Mozilla" to "user-agent=IE" which would check the text/html version of the page for validation errors. As for "user-agent=W3C_Validator" this would be for someone who goes directly to the validator and types (or pastes) the page's normal URL. In the case of my site, this would cause the validator to check the page as "XHTML 1.1 plus MathML 2.0 plus SVG 1.1" using the xhtml mime type. With a few additions, this code can be on any page from html 4.01 to xhtml 1.1 as well as future x/html doctypes thanks to use of the object element rather than the img element ( http://www.xml.com/pub/a/2003/07/02/dive.html ).


I realize that the code displayed in this post is incomplete and still not fully explained, but hopefully it will be helpful to anyone who would like allow their users to validate web pages, based on the mimetype being viewed by the visitor at the time.

@tasairis
Again, thank you for your help, without which I would not have been able to find a solution to this problem.
Post Reply