首页  >  文章  >  php教程  >  PHP判断端口是否打开的代码

PHP判断端口是否打开的代码

PHP中文网
PHP中文网原创
2016-05-25 17:15:022091浏览

跳至

<?php
 2    $host = &#39;www.baidu.com&#39;; //要ping的地址,也可以是IP
 3     $port = &#39;80&#39;; //要ping的端口
 4    $num  = 3;
 5   function microtime_float()
 6   {
 7           list($usec, $sec) = explode(" ", microtime());
 8           return ((float)$usec + (float)$sec);
 9   }
10  function ping($host,$port)
11  {
12          $time_start = microtime_float();
13          $ip = gethostbyname($host);
14          $fp = @fsockopen($host,$port,&$errno,&$errstr,1);
15          if(!$fp) return &#39;replay time out!&#39;;
16          $get = "GET / HTTP/1.1\r\nHost:".$host."\r\nConnection: Close\r\n\r\n";
17          @fputs($fp,$get);
18          @fclose($fp);
19          $time_end = microtime_float();
20          $time = $time_end - $time_start;
21          $time = ceil($time * 1000);
22          return &#39;Reply from &#39;.$ip.&#39;: time=&#39;.$time.&#39;ms
&#39;;
23  }
24  echo &#39;Pinging &#39;.$host.&#39; [&#39;.gethostbyname($host).&#39;] with Port:&#39;.$port.&#39; of data:

&#39;."\r\n";
25  for($i = 0;$i < $num;$i++)
26  {        
27          ping($host,$port);
28          sleep(1);
29          ob_flush();
30          flush();
31  }
32  ?>

                   

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn