Port checking from php

梦想的初衷 提交于 2019-12-02 07:02:23

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.

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.

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

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