Okay so here it is.
The code below is my player counter for a game called Minecraft. It connects to a server and brings back 2 values. 1) The number of current online players and 2) the maximum number of player slots.
Then it stores these two variables in to SQL against the server so that I may use them anywhere on the website like: 'Player: 40/100'.
Now the problem is that it takes far too long for one server to reply that it drops out. 8 - 10 servers are updated but the rest are left. What would be great is a method of starting the process, getting 1 ip, get the results, store the results, close the process and repeat. This does them all on one execution.
The perfect script for my needs would:
1) Open a connection to IP #1 allowing 5000ms for a connection
2) Get the data from IP #1 allowing 5000ms
3) Store the data in SQL
4) Close the connection to IP #1
5) Open a connection to IP #2 allowing 5000ms
6) repeat and so on...
The big question: is there a way to get my server to run this check for a few servers at the same time but starting from ((max_id - min_id) / 2).
So it can do 1000 servers in the same time it takes to do 500 as one script stars from id 1 and the other starts from id 500.
Look forward to some awesome replies :)
Thanks!
<meta http-equiv="refresh" content="180"/>
<?php
if ($_SERVER['SERVER_ADDR'] != '::1'){
echo"You are not authorised to execute this script";
}else{
include('../config.php');
set_time_limit(0);
class MinecraftQuery
{
public static function query($address, $port, $timeout)
{
$socket = @fsockopen($address, $port, $timeout);
if (!$socket) {
echo("Error.");
mysql_query("UPDATE servers SET cur_players='0' WHERE ip='".$address."';");
}else{
fwrite($socket, chr(254));
$response = "";
while(!feof($socket)) $response .= fgets($socket, 1024);
//$result = array();
$response = str_replace(chr(0),"",$response);
$response = substr($response, 2);
$query = preg_split("[".chr(167)."]", $response);
//$result['hostname'] = ($query[0]);
$result ['Players'] = (int) $query[1];
$result['/'] = (int) $query[2];
echo($query[1]."/".$query[2]);
mysql_query("UPDATE servers SET cur_players=".$query[1].", max_players=".$query[2]." WHERE ip='".$address."';");
}
}
}
$servers = mysql_query("SELECT server_name, port, ip FROM servers where enabled = 1 AND active= 1;");
$num_rows = mysql_num_rows($servers);
if ($num_rows > 0) {
while ($serv = mysql_fetch_array($servers)) {
echo(''.$serv['server_name'].' : ');
MinecraftQuery::query($serv['ip'], $serv['port'], 2);
echo('<br /><br />');
}
}
header('Location: player_count.php');
}
?>