当前位置:Gxlcms > PHP教程 > PHP判断服务器端口是否打开的代码_PHP教程

PHP判断服务器端口是否打开的代码_PHP教程

时间:2021-07-01 10:21:17 帮助过:6人阅读

记录一下,以后备用

  1. php
  2. $host = www.google.com; //要ping的地址,也可以是IP
  3. $port = 80; //要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 replay time out!;
  16. $get = "GET / HTTP/1.1 Host:".$host." Connection: Close ";
  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 Reply from .$ip.: time=.$time.ms<br />;
  23. }
  24. echo Pinging .$host. [.gethostbyname($host).] with Port:.$port. of data:<br /><br />." ";
  25. for($i = 0;$i < $num;$i++)
  26. {
  27. ping($host,$port);
  28. sleep(1);
  29. ob_flush();
  30. flush();
  31. }
  32. ?>

本文摘自 http://www.cnblogs.com/luoine/archive/2010/12/01/1893156.html

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/486053.htmlTechArticle记录一下,以后备用 ? php $ host = www.google.com ;//要ping的地址,也可以是IP $ port = 80 ;//要ping的端口 $ num = 3 ; functionmicrotime_float() { list($usec,$s...

人气教程排行