Port checking from php

点点圈 提交于 2019-12-02 06:40:35

问题


I'm trying to connect to gmail pop server from a phplist installation and it fails, but i'm not sure whether my webhost opened port 995 or not. They say they have opened it, but i'm in doubt. Is there a way i can check it from a php script? They are running php 5.2.0 on a windows server, though i'm not sure what OS is that. phpinfo() says "Windows NT DEDI514 5.2 build 3790"


回答1:


You can put code in a php script to open a connection to a specific hostname (or IP address) and port. If you know the expected response, you should be able to tell if you are getting a connection. If you get something like "Connection refused", then either you are being blocked, or the destination host is not accepting connections on that port.

This example uses IP address 192.0.2.0 and port 995. Replace these with whatever you want to test.

<?php
    echo "\nOpening connection\n\n";

    $fp = fsockopen("192.0.2.0", 995, $errno, $errstr);
    if (!$fp) {
        echo "ERROR: $errno - $errstr\n";
    } else {
        echo fread($fp, 1024);
        fclose($fp);
    }
?>

You can also send data to the server using

fwrite($fp, "blah blah blah\r\n");

There is more information about fsockopen here.




回答2:


I think you'll need to ping or traceroute to a machine that will respond on that port.

This article should have much more than you want to know, but there's an example script at the bottom that you can modify to test.

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=8&txtCodeId=1786

There are some other scripts here: http://www.theworldsend.net/

I can't vouch for any of these personally, but they look like what you need.

And, of course, if you can ssh or telnet into your server, you can do all this much more easily using the ping and traceroute commands.




回答3:


Maybe safe mode is active? This prevents calling services on other servers.

Edit: All filesystem and stream functions are affected by the safe mode settings!

The open_basedir setting affects fopen()!



来源:https://stackoverflow.com/questions/1758580/port-checking-from-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!