Search code examples
phpfsockopen

PHP: Most efficient way to make multiple fsockopen(); connections?


Hey guys i'm making a website where you submit a server for advertising. When the user goes to the index page of my website it grabs the ip's of all the servers submitted and then tests to see if it is online using fsockopen() like so:

while($row = mysql_fetch_assoc($rs)) {

    $ip = $row['ip'];

    $info = @fsockopen($ip, 25565, $errno, $errstr, 0.5);

     if($info) {
    $status = "<div><img width='32px' height='32px' 
            title='$name is online!' src='images/online.png'/></div>";

    $online = true;

    } else {

    $status = "<div><img width='32px' height='32px' 
            title='$name is offline!' src='images/offline.png'/></div>";

    $online = false;
    }

}

}

This way works fine, but the only downside is when you load the site it takes a good 2-4 seconds to start loading the website due to the fsockopen() methods being called. I want to know if there is a better way to do this that will reduce the amount of wait time before the website loads.

Any information will be appreciated, thanks.


Solution

  • Store the online status and last check time in a database, if the last check time is longer than 15 minutes for example, update it. I am pretty sure you don't need to get the status on EVERY pageload? It's the time it takes to connect to each server that slows down the website.

    Then again, you would probably wanna move the update process to a cronjob instead of relying on someone visiting your website to update the server statuses.