《PHP實例:php實現的ping端口函數實例》要點:
本文介紹了PHP實例:php實現的ping端口函數實例,希望對您有用。如果有疑問,可以聯系我們。
PHP學習本文實例講述了php實現的ping端口函數.分享給大家供大家參考.
PHP學習具體實現代碼如下:
代碼如下:
<?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,$port,&$errno,&$errstr,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 '<font color="#FF0000">'.getsoft($host,$port).'</font>';
??????????? 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);
??????????? }
??? }
?>
<form method="POST">
域名/IP:<input type="text" name="url" value="<?php echo $host;?>" size="50">
端口:<input type="text" name="duankou" value="<?php echo $port;?>" size="10">
<input type="submit" value="ping">
</form>
PHP學習希望本文所述對大家的PHP程序設計有所贊助.
歡迎參與《PHP實例:php實現的ping端口函數實例》討論,分享您的想法,維易PHP學院為您提供專業教程。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/14084.html