note 37211 added to function.fsockopen
| From: | telefoontoestel59 at spamdothotmail dot com | Date: | Thu, 06 Nov 2003 19:45:25 +0000 |
| Subject: | note 37211 added to function.fsockopen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-60018@lists.php.net to get a copy of this message | ||
I was looking for a function to ping an server or ip adress. I could only find the system function,
but that works really slow on my machine (and server aswell). I now wrote this function (included in
the script) wich can send an amount of data to an ip adress (and can resolve adresses etc also). I
only am strugling with the time out parameter in the fsockopen function. For some reason it
won't function here. If there is a mistake in the usage please send a mail to me and I can
correct it :).
<?php
// Script created by telefoontoestel
// for info of some kind please send an email to
// telefoontoestel at officexpert dot digitaal dot biz
// get_microtime() function copied from php.net: function microtime() user contributes
// get_microtime function
function get_microtime(){
list($usec, $sec) = @split(" ", microtime());
return ((float)$usec + (float)$sec);
}
// end of get_microtime function
// ping function
function ping($server, $num_pings=4, $size=32, $time_out=15){
if(!is_int($num_pings) || $num_pings < 0) $num_pings = 4;
if(!is_int($size) || $size < 0) $size = 32;
if(!is_int($time_out) || $time_out < 0) $time_out = 15;
$results = Array();
for($i = 0; $i < $size; $i++){
$data .= "\0";
}
$server_ip = gethostbyname($server);
$server_name = $server;
if($server_ip != $server_name) $results[0]["server_name"] = $server_name."
[".$server_ip."]";
else $results[0]["server_name"] = $server_ip;
$results[0]["server_ip"] = $server_ip;
$results[0]["bytes"] = $size;
for($i = 1; $i <= $num_pings; $i++){
$start_timer = get_microtime();
if(($handle = @fsockopen("udp://".$server_ip, 7, $errno, $errstr, $time_out)) == false)
if(($handle = @fsockopen($server_ip, 7, $errno, $errstr, $time_out)) == false){
$results[$i]["bytes"] == 0;
$results[$i]["time"] == -1;
continue;
}
@fputs($handle, $data, $size);
$results[$i]["bytes"] = @fgets($handle, $size);
$results[$i]["time"] = round((get_microtime() - $start_timer) * 1000);
$start_timer = 0;
@fclose($handle);
}
return $results;
}
// end of ping function
if(($server = $_GET["server"]) == "") $server = "127.0.0.1";
$result = ping($server);
$lost = 0;
$received = 0;
$send = sizeof($result) - 1;
$max_time = 0;
$min_time = 15000;
$avg_time = 0;
$total_time = 0;
print "Pinging ".$result[0]["server_name"]." with
".$result[0]["bytes"]." bytes of data:<br><br>\n";
for($i = 1; $i < sizeof($result); $i++){
if($result[$i]["time"] == -1){
print "Request timed out<br>\n";
$lost++;
continue;
}
$total_time += $result[$i]["time"];
$received++;
if($result[$i]["time"] > $max_time) $max_time = $result[$i]["time"];
else if($result[$i]["time"] < $min_time) $min_time = $result[$i]["time"];
print "Reply from ".$result[0]["server_ip"].":
bytes=".$result[0]["bytes"]." time=".$result[$i]["time"]."ms
<br>\n";
}
$avg_time = round($total_time / $send);
print "<br>\n";
print "Ping statistics for ".$result[0]["server_ip"].":<br>\n";
print " Packets: Sent = ".$send.", Received =
".$received.", Lost = ".$lost." (".round((100 / $send) * $lost)."%
loss)<br>\n";
print "Approximate roundtrip in milli-seconds:<br>\n";
print " Minimum = ".$min_time."ms, Maximum =
".$max_time."ms, Average = ".$avg_time."ms<br>\n";
?>
----
Manual Page -- http://www.php.net/manual/en/function.fsockopen.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+37211
Delete -- http://master.php.net/manage/user-notes.php?action=delete+37211&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+37211&report=yes
Search -- http://master.php.net/manage/user-notes.php