【问题标题】:Checking whether or not a port is reachable with PHP检查是否可以使用 PHP 访问端口
【发布时间】:2014-02-24 07:33:06
【问题描述】:

我正在尝试检查我的服务器上的端口是否打开,我有一些 php 可以做到这一点

<?php    
$host = 'xxxxxxxxxxxxxxxxxxx';
    $ports = array(xxxxxx);

    foreach ($ports as $port)
    {
        $connection = @fsockopen($host, $port);

        if (is_resource($connection))
        {
            echo '<h2>' . $host . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.</h2>' . "\n";

            fclose($connection);
        }

        else
        {
            echo '<h2>' . $host . ':' . $port . ' is not responding.</h2>' . "\n";
        }
    }
    ?>

输出如下:

domain.zapto.org:80 (http) 已打开。 domain.zapto.org:8090 不是。 回应。 domain.zapto.org:34134 没有响应。

但是如果我去 domain.zapto.org:34134 它工作正常....所以它是可以访问的,但为什么它说它不是?有任何想法吗?谢谢各位。

【问题讨论】:

    标签: php connection ports


    【解决方案1】:

    通过提供fsockopen 函数附加参数为您的代码添加基本调试(参见manual

    $host = 'theofilaktoshouse.zapto.org';
    $ports = array(80, 8090, 34134);
    
    foreach ($ports as $port)
    {
        $errno  = null;
        $errstr = null;
    
        $connection = @fsockopen($host, $port, $errno, $errstr);
    
        if (is_resource($connection)) {
            echo '<h2>' . $host . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.</h2>' . "\n";
            fclose($connection);
        } else {
            echo "<h2>{$host}:{$port} is not responding. Error {$errno}: {$errstr} </h2>" . "\n";
        }
    }
    

    原因它可能不起作用的原因是您的托管服务商设置的交通规则。他们可以轻松禁止到 80 以外端口的传出连接

    【讨论】:

    • 感谢您的回复。错误 110:连接超时
    • 然后尝试通过添加另一个参数(如@fsockopen($host, $port, $errno, $errstr, $timeout);
    猜你喜欢
    • 2014-08-21
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 2021-01-27
    • 1970-01-01
    • 2017-07-30
    相关资源
    最近更新 更多