Home  >  Article  >  Backend Development  >  php怎么实现这样的效果,大家来讨论下

php怎么实现这样的效果,大家来讨论下

WBOY
WBOYOriginal
2016-06-13 13:31:38836browse

php如何实现这样的效果,大家来讨论下。
检测是否能ping通201.168.56.17 。如果可以ping通。。echo (延迟是多少)
如果ping不通 》返回 echo("ping return ;")

就是检测对方主机是否运行。。。

大牛来指点一二。新手来学习一下。大家一起讨论下啦

------解决方案--------------------
搜到一段代码:

PHP code
<?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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn