At 01:45 AM 1/24/2002 +0100, Frank Benady wrote:
>Hi All
>
>Can I emulate the first request of a browser when it tries to connect to a
>distant server using a domain name and parse the answer from the remote
>server to know if there is a website located there or if there is no hosting
>(or even some kind of forwarding) configured for this domain name ?
>Is ther some http functions in PHP which permit to do this test ?

You can use fsockopen() to open a socket connection to a remote host via 
port 80 (HTTP).  If fsockopen() returns a valid file pointer then that 
means there is something listening on port 80 on the host (99% it will be a 
web server).

You could then use fputs() to send a request to the server, such as:

"HEAD / HTTP/1.0\r\n\r\n"

Normally browsers use GET instead of HEAD, but in your case you are only 
interested in the servers response (the HTTP headers) and not the actual 
file/page itself.

You could then use fgets() to read the response sent back from the server 
and parse it to get the information you wanted (for example, look for a 
"Location: ..." line in the headers to see if the page is trying to 
redirect the browser).

I have a function that checks to see if a particular file is available via 
HTTP from a remote host.  It takes a full URL and returns true if the 
page/file exists, and false if it doesn't.  With some work you could modify 
this script to achieve what you want.  I'm posting it below.  Ask if you 
have any questions about it.

Oh, BTW, I'm sure that I have (as usually) went totally overboard with this 
function and someone will now probably point out that PHP has something 
built in to do what I'm doing here...but I couldn't find it and I had fun 
writing this function anyway. :-)

<?
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