note 47495 added to function.fsockopen
| From: | SimonRigetatparagi dot com at osu1 dot php dot net | Date: | Thu, 18 Nov 2004 04:25:35 +0000 |
| Subject: | note 47495 added to function.fsockopen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-80685@lists.php.net to get a copy of this message | ||
Ping from PHP script. (Without system call)
This is of cause not a real ping since there is no support for ICMP. So it can't send a packet
and get it back. But the important thing is to get a response from the server, even if it is only a
ICMP error message. That way it verifies the servers existens and calculate the round trip time,
which is the essential function of ping. It work well on all servers I have tried, with the echo
service running.
<?
$host='www.php.com';
$timeout=2;
//Open the socket
$handle=fsockopen('ddp://'.$host, 7, $errno, $errstr, $timeout);
if (!$handle)
echo "$errstr ($errno)<br>\r\n";
else {
//Set read timeout
stream_set_timeout($handle, $timeout);
for($i=0;$i<3;$i++){
//Time the responce
list($usec, $sec) = explode(" ", microtime(true));
$start=(float)$usec + (float)$sec;
//send somthing
$write=fwrite($handle,"echo this\n");
if(!$write){
echo "Error in writing to socked<br>\r\n";
break;
}
echo 'Send packet to '.$host;
//Try to read. the server will most likely respond with a "ICMP Destination Unreachable"
and end the read. But that is a responce!
fread($handle,1024);
//Work out if we got a responce and time it
list($usec, $sec) = explode(" ", microtime(true));
$laptime=((float)$usec + (float)$sec)-$start;
if($laptime>$timeout)
echo " : No reply<br>\r\n";
else
echo " : Round trip = ".round($laptime,3)." s<br>\r\n";
}
fclose($handle);
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.fsockopen.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47495
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47495&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47495&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47495&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47495&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47495&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47495&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47495&report=yes
Search -- http://master.php.net/manage/user-notes.php