note 16370 deleted from function.fsockopen by jimw

From: Date: Sat, 27 Oct 2001 21:40:42 +0000
Subject: note 16370 deleted from function.fsockopen by jimw
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-19515@lists.php.net to get a copy of this message
There doesn't seem to be a foolproof way of opening a list of Web pages to make sure they're there (for instance, if you have a directory site and need to confirm from time to time that each page is actually there). The functions file() and fopen() don't work on domains that have URL forwarding. Thankfully, fsockopen() does, but only if you write a single header line to the connection, like this... fputs($fp,"HEAD $path HTTP/1.0\r\n\r\n"); However, some servers require that you also write a 'Host' header to the connection before they will respond, so for these servers, you must write... fputs($fp,"HEAD $path HTTP/1.0\r\n"); fputs($fp,"Host: $server\r\n\r\n"); The problem is that if you write the 'Host' header to a domain with URL forwarding, it responds with a 302 MOVED TEMPORARILY error. The only solution I've found is to try one method, then the other if the first one doesn't work. I would not categorize myself as an expert in this area, so if anyone has a better solution, please let me know. I'd love to hear it. Here is the code I'm using (No problems with it yet): <? $server_array[]="powerheroes.com"; $server_array[]="www.heroxero.com"; $path="/"; foreach ($server_array as $server) { echo "Trying $server... "; if (!$fp=fsockopen("$server",80,&$errno,&$errdesc,30)) { // CONNECTION FAILED echo "Error: $errno\n"; continue; } else { // CONNECTION SUCCEEDED fputs($fp,"HEAD $path HTTP/1.0\r\n"); fputs($fp,"Connection: close\r\n\r\n"); if (!$response=chop(fgets($fp,1024))) { // SERVER DIDN'T RESPOND fclose($fp); if (!$fp=fsockopen("$server",80,&$errno,&$errdesc,30)) { // TRY RECONNECTING echo "Error: $errno\n"; continue; } else { // PROVIDE THE SERVER WITH THE HOST INFO fputs($fp,"HEAD $path HTTP/1.0\r\n"); fputs($fp,"Host: $server\r\n"); fputs($fp,"Connection: close\r\n\r\n"); $response=chop(fgets($fp,1024)); } } echo "Response: $response\n"; // PRINT THE RESPONSE fclose($fp); } } ?> (Sorry if the code is hard to read - I can't figure out how to indent it here)

« previous php.notes (#19515) next »