• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Simple OTServer status

Raggaer

Godly Member
Joined
Jul 25, 2012
Messages
1,557
Solutions
8
Reaction score
957
Location
Spain
Well since lots of people actually asked me how can this be done I decided to make a very simple function to get the basic info from a server

Keep in mind this function doesnt even check if server is offline

PHP:
Class Status
{
    public $info = array();

    public function LoadStatus($ip)
    {
        @$sock = fsockopen ($ip, '7171', $errno, $errstr, 1);
        $info = chr(6).chr(0).chr(255).chr(255).'info';
        fwrite($sock, $info);
        $data='';
        while (!feof($sock))$data .= fgets($sock, 1024);
        fclose($sock);

        $stats = simplexml_load_string($data);
        $this->info['ip'] = $ip;
        $this->info['uptime'] = $stats->serverinfo->attributes()->uptime;
        $this->info['servername'] = $stats->serverinfo->attributes()->servername;
        $this->info['location'] = $stats->serverinfo->attributes()->location;
        $this->info['distro'] = $stats->serverinfo->attributes()->server;
        $this->info['version'] = $stats->serverinfo->attributes()->version;
        $this->info['client'] = $stats->serverinfo->attributes()->client;
        $this->info['owner'] = $stats->owner->attributes()->name;
        $this->info['email'] = $stats->owner->attributes()->email;
        $this->info['players'] = $stats->players->attributes()->online;
        $this->info['pmax'] = $stats->players->attributes()->max;
        $this->info['ppeak'] = $stats->players->attributes()->peak;
        $this->info['motd'] = $stats->motd;
        $this->info['on'] = 1;

        return $this->info;
    }
}

As you can see its very basic but should do the job

Quick example:

PHP:
$status = new Status;
$info = $status->LoadStatus('yourserver.net');

echo 'Players online : ' . $info['players'];
 
Nice.

Here is more useful as for me class.
Code:
<?php
class Status
{
    public $data = array(
        'host' => null,
        'port' => null,
        'uptime' => 0,
        'servername' => null,
        'location' => null,
        'distro' => null,
        'version' => null,
        'client' => null,
        'owner' => null,
        'email' => null,
        'players' => 0,
        'pmax' => 0,
        'ppeak' => 0,
        'on' => false
    );
   
    private function _load_data($host, $port)
    {
        @$sock = fsockopen ($ip, $port, $errno, $errstr, 1);
        $info = chr(6).chr(0).chr(255).chr(255).'info';
        fwrite($sock, $info);
        $response = "";
        while (!feof($sock)) $response .= fgets($sock, 1024);
        fclose($sock);
       
        if ($sock) {
            $xml = simplexml_load_string($response);
            $this->data['host'] = $host;
            $this->data['port'] = $port;
            $this->data['uptime'] = $xml->serverinfo->attributes()->uptime;
            $this->data['servername'] = $xml->serverinfo->attributes()->servername;
            $this->data['location'] = $xml->serverinfo->attributes()->location;
            $this->data['distro'] = $xml->serverinfo->attributes()->server;
            $this->data['version'] = $xml->serverinfo->attributes()->version;
            $this->data['client'] = $xml->serverinfo->attributes()->client;
            $this->data['owner'] = $xml->owner->attributes()->name;
            $this->data['email'] = $xml->owner->attributes()->email;
            $this->data['players'] = $xml->players->attributes()->online;
            $this->data['pmax'] = $xml->players->attributes()->max;
            $this->data['ppeak'] = $xml->players->attributes()->peak;
            $this->data['motd'] = $xml->motd;
            $this->data['on'] = true;
        }
    }

    public function __construct($host, $port = 7171)
    {
       $this->_load_data($host, $port);
    }
   
    public function connect($host, $port = 7171)
    {
        $this->_load_data($host, $port);
    }
   
    public function isOnline()
    {
        return (bool) $this->data['on'];
    }
   
    public function __get($field)
    {
        if (array_key_exists($field, $this->data))
            return $this->data[$field];
    }
   
    public function __set($field, $value)
    {
        if (array_key_exists($field, $this->data))
            $this->data[$field] = $value;
    }
}

Example:
Code:
$status = new Status('server.net', 7171);
echo 'Server is ' . (($status->isOnline()) ? 'online' : 'offline');
echo 'Players online : ' . $status->players;
$status->connect('second.server.net', 7171); // if want to check other server or simply create new instance of status
 
Hello, can you explain why this code is needed for?

PHP:
$info = chr(6).chr(0).chr(255).chr(255).'info';
fwrite($sock, $info);
 

Similar threads

Back
Top