逐步输出exec()ping结果 [英] Outputting exec() ping result progressively

查看:265
本文介绍了逐步输出exec()ping结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个可对数百个地址执行ping操作并返回其值(毫秒)的函数.到目前为止,我已经实现了最初的想法,即可以ping并获得结果,但是问题是,当对数百个地址使用相同的代码时,PHP页面将暂停直到它超时或到达最后一个ping命令.

I'm trying to write a function that pings a few hundred addresses and returns their values (milliseconds). So far I've achieved the initial idea which is to ping and get the result but the problem arises when using the same code for hundreds of addresses, the PHP page stalls until it either times out or reaches the last ping command.

如果能得到一些逐步输出结果的建议,我将感到非常高兴,这是我当前的代码:

I would be glad if I could get some suggestions to output the results progressively, here is my current code:

<?php

// "for" loop added according to suggestion for browser compatibility (IE, FF, CHR, OPR, SFR)
for($i = 0; $i < 5000; $i++)
{
    echo ' ';
}

function GetPing($ip = NULL) {
    // Returns the client ping if no address has been passed to the function
    if(empty($ip)) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }

    // Check which OS is being run by the client
    if(getenv('OS') == 'Windows_NT') {
        //echo '<b>Detected local system:</b> Windows NT/2000/XP/2003/2008/Vista/7<p>';
        $exec = exec("ping -n 1 -l 32 -i 128 " . $ip);
        return end(explode(' ', $exec));
    }
    else {
        //echo '<b>Detected local system:</b> Linux/Unix<p>';
        $exec = exec("ping -c 1 -s 32 -t 128 " . $ip);
        $array = explode('/', end(explode('=', $exec )));
        return ceil($array[1]) . 'ms';
    }
    // ob_flush and flush added according to suggestion for buffer output
    ob_flush();
    flush();
}

// Added to test 20 sequential outputs
for($count = 0; $count < 20; $count++)
    echo GetPing('8.8.8.8') . '<div>';

?>

在获得一些反馈之后,我在脚本中添加了一个 for 循环以及ob_flush()和flush(),并且还在 php.ini中将output_buffering设置为0 .到目前为止,我似乎测试过的大多数浏览器(IE8,Firefox 12,Chrome 19,Opera 11,Safari 5)似乎都可以使用.似乎当前的代码现在可以按预期工作,但是任何改进的建议都将受到赞赏.

After some feedback, I've added a for loop as well as ob_flush() and flush() to my script and I've also set output_buffering to 0 in php.ini. It seems to work for most browsers that I tested so far (IE8, Firefox 12, Chrome 19, Opera 11, Safari 5). It seems the current code is now working as intended but any suggestion to improve on it is immensely appreciated.

感谢您的反馈.

推荐答案

这只是一个猜测;几年前,我写了一个非常不稳定的聊天脚本,该脚本也使用了输出缓冲(并在while(true)循环中获取新消息).

this is just a guess; I've written years ago a very wobbly chat script that used output buffering as well (and fetching new messages in while(true) loop) ..

在执行此操作时,我遇到了相同的问题,有时脚本停顿(黑屏),有时花了一段时间才出现字符,而且这也是特定于浏览器的.

While doing this I've encountered the same problems that sometimes the script stalled (blank screen), sometimes it took a while until the characters appeared and additionally this was also browser specific.

以下是我添加到脚本中的相关代码段,以使其可与IE6和FF2配合使用(正如我几年前所说的那样……)

Here are the relevant code snippets I've added to the script to have it work with IE6 and FF2 (as I said, years ago ...)

<?php
    // End output buffering
    ob_end_flush(); 

    // IE and Safari Workaround
    // They will only display the webpage if it's completely loaded or
    // at least 5000 bytes have been "printed".
    for($i=0;$i<5000;$i++)
    {
        echo ' ';
    }

    while( ... )
    {
        echo 'Message';
        ob_flush();
        flush();
    }
?>

它对我有用,所以也许您也可以尝试一下. (尽管我不知道现代的浏览器和服务器基础设施将如何表现出来).

It worked for me, so maybe you could give it a try as well. (Altough I have no idea how modern browsers and server infrastrucutre will behave to this).

这篇关于逐步输出exec()ping结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆