dream07769 2019-06-10 15:54
浏览 116

PHP中fsockopen的非阻塞替代方案?

I've read lots of answers but I haven't found a clear explanation. I'm trying to make an open port check tool in PHP.

When using fsockopen the server stops working while it checks every single port, so I cannot use this method to check the ports status.

Is there any efficient way of doing the same thing but in a non-blocking or asynchronous way?

This is my (non efficient) code:

        $resultList = [];
        foreach ($list as $key => $value) {
             $object = new stdClass();
             $object->id = $value["id"];

            if (fsockopen($value["ip"], $value["port"])) {
                 $object->status = true;
            } else {
                $object->status = false;
            }
            array_push($resultList,$object);
        }
  • 写回答

1条回答 默认 最新

  • dpojoxa5613 2019-06-10 16:30
    关注

    A PHP script is always synchronous - you call a function, you wait for it to finish before your script moves on, however you can use PHP to trigger a second PHP script as a new process via the following generalised command:

    shell_exec('/usr/bin/php -f /path/to/script.php &> /dev/null &');
    

    Now this doesn't really help your problem I just included it to answer the exact words of your question - you can't have this second PHP script interact with the first in any way without involving AJAX, at which point you may as well just AJAX the entire thing.


    To try and answer the spirit of your question I would like to suggest a different approach:

    You have your parent page:

    <?php
    
    $hostname = '192.168.0.1';
    
    ?>
    <h1>Ports for server <?php echo $hostname; ?>:</h1>
    <iframe src="/path/to/my/post-scanner-script.php?hostname=<?php echo urlencode($hostname); ?>"></iframe>
    

    Then your child page scans each port in sequence, and as it has finished scanning that port it uses flush()

    <?php
    
    // Stuff here to interpret $_GET['hostname'];
    
    foreach ($list as $key => $value) {
        $status = !!fsockopen($value["ip"], $value["port"]);
        echo "<div>{$value['id']}: " . ($status ? 'OPEN' : 'CLOSED') . "</div>";
        flush();
    }
    

    What flush() will do is immediatley send all rendered output to the client machine (it will not send the same content twice), so what you will get is a slowly appearing list of each port and its status, appearing as soon as your script knows the status of the port.

    This provides some immediate feedback to the end user as well as a less frustrating wait (it's much more bearable when you can actually see progress being made).

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测