note 47495 modified in function.fsockopen by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 19:48:41 +0000 |
| Subject: | note 47495 modified in function.fsockopen by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142312@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.
<?php
$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);
}
?>
--was--
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);
}
?>
http://php.net/manual/en/function.fsockopen.php