富贵资源网 Design By www.hznty.com

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

具体实现代码如下:

复制代码 代码如下:
<"content-Type: text/html; charset=utf-8");
    set_time_limit(120);
    $host = isset($_POST['url']) " ", 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);
            }
    }
"POST">
域名/IP:<input type="text" name="url" value="<" size="50">
端口:<input type="text" name="duankou" value="<" size="10">
<input type="submit" value="ping">
</form>

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

富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com