Question

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';
        }
    }
Was it helpful?

Solution

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!

OTHER TIPS

Are the three hosts you quoted above all on the same machine, or are they on separate machines? It could be that the configurations between them are not consistent. Check to see if 'fsockopen' is enabled:

 <?php
 if(function_exists('fsockopen')) {
      echo "fsockopen function is enabled";
 }
 else {
      echo "fsockopen is not enabled";
 }
 ?>

If it is enabled, check to see if firewall rules are blocking the host that is not successful.

Faililng that, migrate your script to a cURL based implementation: http://ch.php.net/curl and see if that helps to resolve the issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top