php实现的ping端口函数实例

前端之家收集整理的这篇文章主要介绍了php实现的ping端口函数实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了PHP实现的ping端口函数分享给大家供大家参考。

具体实现代码如下:

代码如下:
PHP /* * @author xujiajay * @date 2010-10-7 * @function 可以ping端口的PHP函数 * */ error_reporting(E_ERROR); header("content-Type: text/html; charset=utf-8"); set_time_limit(120); $host = isset($_POST['url']) ? chop(str_replace('http://','',$_POST['url'])) : 'www.baidu.com'; $port = isset($_POST['duankou']) ? chop($_POST['duankou']) : '80'; $num = 10; function microtime_float() { list($usec,$sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); } function getsoft($host,$port) { $fp = @fsockopen($host,$port,&$errno,&$errstr,3); if(!$fp) return 'unknown'; $get = "GET / HTTP/1.1\r\nHost:".$host."\r\nConnection: Close\r\n\r\n"; @fputs($fp,$get); $data = ''; while ($fp && !feof($fp)) $data .= fread($fp,1024); @fclose($fp); $array = explode("\n",$data); $k = 2; for($i = 0;$i < 20;$i++) { if(stristr($array[$i],'Server')){$k = $i; break;} } if(!stristr($array[$k],'Server')) return 'unknown'; else return str_replace('Server','服务器软件',$array[$k]); } function ping($host,$port) { $time_start = microtime_float(); $ip = gethostbyname($host); $fp = @fsockopen($host,1); if(!$fp) return 'Request timed out.'."\r\n"; $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'; } if(isset($_POST['url']) && isset($_POST['duankou'])) { echo ''; echo 'Pinging '.$host.' ['.gethostbyname($host).'] with Port:'.$port.' of data:'."\r\n"; ob_flush(); flush(); for($i = 0;$i < $num;$i++) { echo ping($host,$port); ob_flush(); flush(); sleep(1); } } ?>

希望本文所述对大家的PHP程序设计有所帮助。

原文链接:https://www.f2er.com/php/23457.html

猜你在找的PHP相关文章