fsockopen() not working on a specific web host?

user470760 picture user470760 · Nov 8, 2011 · Viewed 8.7k times · Source

One of our customers said they have a very simple script which uses fsockopen() to determine whether a server is online or not. They said until recently it worked fine on their website through us, but recently just stopped working, so I am assuming it is a setting.

My first thought was that he was using it as TCP and using the IP/Port for a Game Server, however he and myself both confirmed the script worked fine from various other web hosts.

What could be the issue on ours in particular?

Works: http://crazygfl.xfactorservers.com/query.php?ip=www.xfactorservers.com&port=80

Works: http://crazygfl-stats.com/query.php?ip=109.73.163.231&port=27015

Doesn't Work: http://crazygfl.xfactorservers.com/query.php?ip=109.73.163.231&port=27015

Here is the code...

<?php

    $timeout = (isset($_GET['timeout']) ? ($_GET['timeout'] <= 0 ? 30 : ((int)$_GET['timeout'])) : 30);

    if(empty($_GET['ip'])&&empty($_GET['port'])){
        echo 'No IP/Port specified.';
    }else{
        if(fsockopen($_GET['ip'], $_GET['port'], $errno, $errstr, $timeout) === false){
            echo 'offline';
        }else{
            echo 'online';
        }
    }

Answer

GordonM picture GordonM · Nov 8, 2011

It's possible that the hosting company have disabled the fsockopen function or firewalled outbound requests on whatever port you're trying to test. If that's the case you can ask them to reenable the function or unblock the port, but if they won't then you have very little recourse other than moving to a new host.

BTW, using data directly from user input ($_GET, $_POST, etc) without any kind of validation is an EXTREMELY bad idea!