Port checking from php

2020-04-21 02:31发布

问题:

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()!



标签: php phplist