Re: PHP: how to ping a server ?

From: Date: Tue, 19 Dec 2000 21:23:45 +0000
Subject: Re: PHP: how to ping a server ?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-31077@lists.php.net to get a copy of this message
Hi Paul - Are you actually trying to check an HTTP return status code? You'll have to do something a little more complex - open a connection to the server, send a HTTP request (eg; HEAD), and parse the response. Adapted from example on fsockopen page (untested): function http_status($Server, $Port, $Timeout) { $Status = $Errno = ''; $fp = fsockopen ($Server, $Port, &$Errno, &$Errstr, $Timeout); if ($fp) { # Send HEAD command, parse first returned line to look for # HTTP returned status code. This first line will look # something like: # HTTP/1.1 200 OK fputs ($fp, "HEAD / HTTP/1.0\r\n\r\n"); if (!feof($fp) && $Line = fgets($fp,128)) { $Bits = split('[[:space:]]+', $Line); $Status = $Bits[1]; } fclose ($fp); } return array($Status, $Errno); } $Server = 'www.php.net'; $Port = 80; $Timeout = 10; # seconds list($s,$e) = http_status($Server,$Port,$Timeout); echo $s; For more info: http://www.php.net/manual/function.fsockopen.php You can also set socket timeouts (in PHP4, and possible in late versions of PHP3) so that your program doesn't hang up if the server isn't responding. See the socket_get_status(), socket_set_blocking() and socket_set_timeout() functions. Lastly, there may well be some existing functions available that do this; check out some of the code libraries such as phpbuilder.com. - steve At 7:10 AM -0500 12/19/00, Toby Butzon wrote:
Try running the actual command with backtick operators... ie: $return_val = ping www.host.com; Good luck- Toby Butzon [ criticism spurs improvement ] ----- Original Message ----- From: "Paul" <dragoslav@gmx.co.uk> To: <php-general@lists.php.net> Sent: Tuesday, December 19, 2000 7:02 AM Subject: [PHP] PHP: how to ping a server ? Hello, I'm looking for 3 day's for a PHP code that's pings a server. And i got not yet a succesful code, so maby the The Master's can help. I want the result's something like thise "www.php.net Ping : 200" I hope that The Master's can help me ;) Greets fron Paul
+--- "They've got a cherry pie there, that'll kill ya" ------------------+
| Steve Edberg                           University of California, Davis |
| sbedberg@ucdavis.edu                               Computer Consultant |
| http://aesric.ucdavis.edu/                  http://pgfsun.ucdavis.edu/ |
+-------------------------------------- FBI Special Agent Dale Cooper ---+

« previous php.general (#31077) next »