At 03:04 PM 1/31/2002 +1300, Philip J. Newman wrote:
>Is there anyway using PHP to send a command to a server to get the get 
>eather a 404 or 200 reply?

Here's a function I put together to do just that.  It takes a full URL as 
an argument and returns either boolean true, boolean false, or a string 
error message.  Since it might return a string error you need to check the 
type returned when using it, such as:

<?
if($http_file_exists("http://someurl.tld/page.html";) === true) {
   //do stuff
}
?>

Note the three equals signs that checks for type as well as value.

Anyway, here is the function:

<?
function http_file_exists ($url) {

   if (!preg_match("/^http:\/\/([^\/]+)\/.*$/i",$url,$matches)) {

     return "Error - incorrect format";

   } else {

     $host = $matches[1];
     $fp = fsockopen ($host, 80, $errno, $errstr, 30);

     if (!$fp) return "Error - couldn't connect to host";
       else {
         fputs ($fp, "HEAD $url HTTP/1.0\r\n\r\n");
         $response = fgets ($fp,128);
         fclose ($fp);
         return (eregi("^.+200 OK.+$",$response) ? true : false);
       }
   }
}
?>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to