simple php validation include that works with allow_url_fopen disabled

Hi.

In the interest of open source, here's the contents of a little PHP
include file that I've started using on my website during development.

If you're curious, visit https://cis.biz.tm/ to see its use.

If you find errors, or even things that are "unconventional", please let
me know. I've only been PHP programming for about a year so any advice
is always welcome.

Much of the code is from
http://www.codingforums.com/archive/index.php/t-79832.html
so many thanks must go to "gsnedders". I've just wrapped it up a bit
nicer.

One neat thing about using fsockopen is that it works even with
"allow_url_fopen" disabled in "php.ini.

Cheers,


Jared Crutchfield



<?php

# http://www.codingforums.com/archive/index.php/t-79832.html

function IsValidHTML($HTML)
{
  $fp=fsockopen('validator.w3.org',80,$errno,$errstr,30);
  if (!$fp)
  {
    return False;
  }
  else
  {
    $HTML=rawurlencode($HTML);
    $out="POST /check?;output=xml HTTP/1.0\r\n";
    $out=$out."Host: validator.w3.org\r\n";
    $out=$out."Content-type: application/x-www-form-urlencoded\r\n";
    $out=$out."Content-length: ".(strlen($HTML)+strlen('fragment='))."\r\n";
    $out=$out."Connection: Close\r\n\r\n";
    fwrite($fp,$out."fragment=".$HTML);
    $HTML="";
    while (!feof($fp))
    {
      $HTML=$HTML.fgets($fp,1024);
    }
    $statuskey="X-W3C-Validator-Status:";
    $i=strpos($HTML,$statuskey);
    if ($i>0)
    {
      $j=strpos($HTML,"\n",$i);
      if ($j>0)
      {
        if (strtolower(trim(substr($HTML,$i+strlen($statuskey),$j-$i-strlen($statuskey))))=="valid")
        {
          return True;
        }
      }
    }
  }
  return False;
}

function GetValidHTMLIcon()
{
  return "<img src=\"http://www.w3.org/Icons/valid-html401-blue\" alt=\"HTML 4.01 Validated\">\n";
}

?>

Received on Friday, 26 February 2010 13:49:47 UTC