3

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';
        }
    }
  • are you sure the fsockopen function is not disabled? – Narcis Radu Nov 08 '11 at 08:23
  • could be a smart and savvy person has blocked requests from the host that doesn't work (assuming it's on a different IP) from the others. that would explain the inconsistent behavior – sdolgy Nov 08 '11 at 08:48
  • What do you mean by "works" / "doesn't work" Does it return a message but not the expected one? Does it give no output? Does it timeout? – symcbean Nov 08 '11 at 13:00

2 Answers2

4

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!

GordonM
  • 31,179
  • 15
  • 87
  • 129
  • Actually WE are the host. However, web-hosting is just a side service we offer to our customers free as an added incentive. Unfortunately, our web-master doesn't speak the best english, so it is better if I just approach him with a solution rather than trying to sort it out with him. As the script works with other ports from our hosting, I am leaning towards the latter. Would the port that isn't working have to be specifically firewalled off? –  Nov 08 '11 at 08:43
  • @Brett Powell: Yes, there seem to be (more or less) specific rules that prevent the script on crazy---.----torservers.com from connecting to the game server. If you enter a webserver address and port (e.g. ip=www.heise.de&port=80) it's working fine. Maybe only some standard ports (http,ftp,ssh,...) are allowed by the firewall or only ports below 1024 or something like that. – VolkerK Nov 08 '11 at 09:25
4

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.

sdolgy
  • 6,963
  • 3
  • 41
  • 61
  • Excellent, thank you. Shouldn't something like this work for a libcurl approach? http://pastie.org/2829896 It shows still shows offline using the Game Server port, so I am going to go ahead and check the machine although I think the firewall is disabled AFAIK. –  Nov 08 '11 at 09:23