Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[PHP] Server Status Script

Elite Diviner
Joined
Feb 27, 2012
Messages
446
Reaction score
46
Hey guys, I started working on a server status script for fiesta. Basically the game uses services which broadcast to specific ports when they are running. My only question is, How does this look? So basically i was just wondering if this method is the right one to use and if there is any major flaws I should look into. I am not asking for any answers just hints to point me in the right direction.

PHP:
<?php
function check_port($port) {
    $conn = @fsockopen($_SERVER['SERVER_ADDR'], $port, $errno, $errstr, 0.2);
    if ($conn) {
        fclose($conn);
        return true;
    }
}

function server_report() {
    $report = array();
    $svcs = array('9110'=>'World',
	'9120'=>'Zone1',
	'9122'=>'Zone2',
	'9124'=>'Zone3',
	'9010'=>'Login');
	
    foreach ($svcs as $port=>$service) {
        $report[$service] = check_port($port);
    }
    return $report;
}

$report = server_report();
echo
"<table>
    <tr>
        <td>Server</td>
        <td>Status</td>
  <tr>
        <td>All Servers</td>
        <td>"; echo $report['World']&$report['Zone1']&$report['Zone2']&$report['Zone3']&$report['Login'] ? 'Online' : 'Offline'; echo "</td>
    </tr>
</table>";
?>
 
Joined
Apr 28, 2005
Messages
6,953
Reaction score
2,420
Ideally you don't want this to run every time someone visits your website. You'd get constant requests going out of your server on each page load this is called on, which can eventually have a performance impact.

I would suggest caching the online/offline return and have a cron running every 5 minutes or so to update the status. Now obviously it wont show offline as soon as the server is down, but there is really no reason to have it update any more less 5 minutes between each call.

Otherwise you included the timeout which people seem to leave out, so everything else looks OK to me really other than "$_SERVER['SERVER_ADDR']". Is there any reason you're using this over just setting 'localhost' assuming this is on the same box as the game server?

edit-
Moving to showcase as there isn't really an "issue" to be fixed.
 
Last edited:
Elite Diviner
Joined
Feb 27, 2012
Messages
446
Reaction score
46
I tried to change it to localhost but it always returns offline. Leaving "$_SERVER['SERVER_ADDR']" should be good though right?

Also would i use php headers to cache? i haven't done much work with caching.
 
Praise the Sun!
Member
Joined
Dec 4, 2007
Messages
2,502
Reaction score
986
I tried to change it to localhost but it always returns offline. Leaving "$_SERVER['SERVER_ADDR']" should be good though right?

Try and change the timeout to 3 seconds instead of 0.2.

Also would i use php headers to cache? i haven't done much work with caching.

You'd be using a simple textfile that contains the data. Serializing and deserializing it would do just fine. Here's a sample:
PHP:
<?php
// Cache timeout in milliseconds
define("CACHE_TIMEOUT",    300);

function buildCache() {
    // Should re-check the serverstatus here, using static stuff for now
    $rgServerStatus = array(
        "server1" => "offline",
        "server2" => "online"
    );
    
    $pFile = fopen("cache.txt", "w");
    
    if ($pFile !== false) {
        fputs($pFile, serialize($rgServerStatus));
        fclose($pFile);
    }
    
    return $rgServerStatus;
}

function loadCache() {
    $pFile = fopen("cache.txt", "r");
    $rgServerStatus = array();
    
    if ($pFile !== false) {
        $rgServerStatus = deserialize(fgets($pFile));
        fclose($pFile);
    }
    
    return $rgServerStatus;
}

// Check if cache expired and either load cache or rebuild cache
$rgServerStatus = (filemtime("cache.txt") < (time() + CACHE_TIMEOUT) ? buildCache() : loadCache());
var_dump($rgServerStatus);
?>
 
Banned
Banned
Joined
Feb 9, 2007
Messages
1,313
Reaction score
177
Just a stupid question from me.. Can you like also use this for your VPS or website or anything else if so can you give the code to that i would like to try it out.

No i can't code PHP

Verstuurd van mijn GT-I9070 met Tapatalk
 
Praise the Sun!
Member
Joined
Dec 4, 2007
Messages
2,502
Reaction score
986
Just a stupid question from me.. Can you like also use this for your VPS or website or anything else if so can you give the code to that i would like to try it out.

No i can't code PHP

Verstuurd van mijn GT-I9070 met Tapatalk

You mean modifying the script so it would monitor a website / VPS?
 
Banned
Banned
Joined
Feb 9, 2007
Messages
1,313
Reaction score
177
You mean modifying the script so it would monitor a website / VPS?

That sir is correct so yes.. So i can monitor all my VPS servers and Reseller server.

Verstuurd van mijn GT-I9070 met Tapatalk
 
Praise the Sun!
Member
Joined
Dec 4, 2007
Messages
2,502
Reaction score
986
That sir is correct so yes.. So i can monitor all my VPS servers and Reseller server.

Verstuurd van mijn GT-I9070 met Tapatalk

That'd work, but if you're referring to VPS servers as if you're hosting them, then there's already plenty of other scripts that do that for you such as .
 
Banned
Banned
Joined
Feb 9, 2007
Messages
1,313
Reaction score
177
Thank you for that but i just want a single code and I'm sure soneone here can provide it and if not then it's fine too.

Verstuurd van mijn GT-I9070 met Tapatalk
 
Praise the Sun!
Member
Joined
Dec 4, 2007
Messages
2,502
Reaction score
986
Thank you for that but i just want a single code and I'm sure soneone here can provide it and if not then it's fine too.

Verstuurd van mijn GT-I9070 met Tapatalk

Create your own thread and I'm sure you'll get what you need.
 
Back
Top