php如何实现这样的效果,大家来讨论下。
检测是否能ping通201.168.56.17 。如果可以ping通。。echo (延迟是多少)
如果ping不通 》返回 echo("ping return ;")
就是检测对方主机是否运行。。。
大牛来指点一二。新手来学习一下。大家一起讨论下啦
------解决方案--------------------
搜到一段代码:
<?php $host = '192.168.1.1'; //要ping的地址,也可以是IP
$port = '80'; //要ping的端口
$num = 3;
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function ping($host,$port)
{
$time_start = microtime_float();
$ip = gethostbyname($host);
$fp = @fsockopen($host,$port,&$errno,&$errstr,1);
if(!$fp) return 'replay time out!';
$get = "GET / HTTP/1.1\r\nHost: $host\r\nConnection: Close\r\n\r\n";
@fputs($fp,$get);
@fclose($fp);
$time_end = microtime_float();
$time = $time_end - $time_start;
$time = ceil($time * 1000);
return 'Reply from '.$ip.': time='.$time.'ms<br />';
}
echo 'Pinging '.$host.' ['.gethostbyname($host).'] with Port:'.$port.' of data:<br><br>'."\r\n";
for($i = 0;$i
<br><font color="#e78608">------解决方案--------------------</font><br>用 php 中的 exec() 调用命令来实现 最好。
<br><font color="#e78608">------解决方案--------------------</font><br>$ip = "192.168.1.1"; <br>exec( "ping $ip ", $arr); <br><br>print_r($arr); <br><div class="clear"></div>
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号