>  기사  >  php教程  >  포트에 ping을 보낼 수 있는 PHP 함수

포트에 ping을 보낼 수 있는 PHP 함수

大家讲道理
大家讲道理원래의
2016-11-09 15:00:241280검색

<?php
/*
 * @author     xujiajay
 * @date       2010-10-7
 * @email      xujiaphp@gmail.com
 * @function   可以ping端口的php函数
 *
 */
    error_reporting(E_ERROR);
    header("content-Type: text/html; charset=utf-8");
    set_time_limit(120);
    $host = isset($_POST[&#39;url&#39;]) ? chop(str_replace(&#39;http://&#39;,&#39;&#39;,$_POST[&#39;url&#39;])) : &#39;www.baidu.com&#39;;
    $port = isset($_POST[&#39;duankou&#39;]) ? chop($_POST[&#39;duankou&#39;]) : &#39;80&#39;;
    $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 &#39;unknown&#39;;
            $get = "GET / HTTP/1.1\r\nHost:".$host."\r\nConnection: Close\r\n\r\n";
            @fputs($fp,$get);
            $data = &#39;&#39;;
            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],&#39;Server&#39;)){$k = $i; break;}
            }
            if(!stristr($array[$k],&#39;Server&#39;)) return &#39;unknown&#39;;
            else return str_replace(&#39;Server&#39;,&#39;服务器软件&#39;,$array[$k]);
    }
    function ping($host,$port)
    {
            $time_start = microtime_float();
            $ip = gethostbyname($host);
            $fp = @fsockopen($host,$port,&$errno,&$errstr,1);
            if(!$fp) return &#39;Request timed out.
    &#39;."\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 &#39;Reply from &#39;.$ip.&#39;: time=&#39;.$time.&#39;ms
    &#39;;
    }
    if(isset($_POST[&#39;url&#39;]) && isset($_POST[&#39;duankou&#39;]))
    {
            echo &#39;<font color="#FF0000">&#39;.getsoft($host,$port).&#39;</font>
      
    &#39;;
            echo &#39;Pinging &#39;.$host.&#39; [&#39;.gethostbyname($host).&#39;] with Port:&#39;.$port.&#39; of data:
      
    &#39;."\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>

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.