[WEB] [PHP] Simple to use minecraft server status query!

Discussion in 'Bukkit Tools' started by FunnyItsElmo, Apr 29, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    FunnyItsElmo

    Minecraft Server Status Query, written in PHP, with online players, motd, favicon and more server related informations without plugins and enable-query.

    Tested with Spigot 1.9 and Bungeecord 1.9 & 1.8

    https://github.com/FunnyItsElmo/PHP-Minecraft-Server-Status-Query

    Installation
    Code:
    composer require funnyitselmo/minecraftserverstatus

    Tutorial
    Code:
    use MinecraftServerStatus\MinecraftServerStatus;
    
    require '../vendor/autoload.php';
    
    $response = MinecraftServerStatus::query('lostforce.com', 25565);
    
    if (! $response) {
        echo "The Server is offline!";
    } else {
        echo "<img width=\"64\" height=\"64\" src=\"" . $response['favicon'] . "\" /> <br>
            The Server " . $response['hostname'] . " is running on " . $response['version'] . " and is online,
            currently are " . $response['players'] . " players online
            of a maximum of " . $response['max_players'] . ". The motd of the server is '" . $response['description'] . "'.
            The server has a ping of " . $response['ping'] . " milliseconds.";
    }
    If the server is offline MinecraftServerStatus::query returns false else it returns an array which contains the server informations.

    Variables
    The table contains the aviable variables the response can contain.
    The default content of each variable is false.


    'hostname' Exact server address in 127.0.0.1 format
    'port' The servers port for example 25565
    'ping' The time in ms the server needs to answer

    'version' The server version
    (for example: 1.7.2)
    'protocol' The server protocol (for example: 4)
    'players' Amount of players who are currently online
    'max_players' Number of the slots of the server
    'description' The message of the day of the server
    'description_raw' The raw version of description (contains color codes etc.)
    'favicon' The favicon of the server in a base64 string (Can be displayed with the html img tag by setting the string as src)

    'modinfo' Informations about the plugins
     
    Last edited: Mar 25, 2016
  2. Offline

    Speaw

    Warning: socket_connect() expects parameter 1 to be resource, null given in /xxx/xxx/public_html/online/status.class.php on line 23
    Warning: socket_create() has been disabled for security reasons in /xxx/xxx/public_html/online/status.class.php on line 22
     
  3. Offline

    FunnyItsElmo

    Speaw you use a free host right? :D On free hosts the script dont work because the socket methods are diabled.
     
  4. Offline

    Speaw

  5. Offline

    FunnyItsElmo

    Then your web server is configured incorrectly or you have an outdated php version.
     
  6. Offline

    Adriani6

    The key line to this error is;
    Warning: socket_create() has been disabled for security reasons in /xxx/xxx/public_html/online/status.class.php on line22

    It means function "socket_create()" is disabled on the host for security reasons lol.

    You should contact your host for more info, if they can enable it for you.
     
  7. Offline

    muffinjello

    Thanks a lot!
     
    tyzoid likes this.
  8. There will be a swarm of kids trying this with no PHP knowledge.
     
    Shortninja66, Cap123, Wruczek and 5 others like this.
  9. Offline

    tyzoid

    Then tell those kids to use a pre-configured server. Awesomestatus: http://mctools.tyzoid.com/mcb/
     
  10. Having some trouble, This is how I have it setup:
    HTML Part:
    HTML:
    <?php
    include_once '/home/content/85/9477285/html/uniqueminecraftservers/listener/MCSS.php';
    $status = new MinecraftServerStatus();
    $response = $status->getStatus('75.127.12.73');
    ?>
    <tr>
            <td class="trank">*</td>
            <td class="tname"><p class="wrap-tname"><a href="server/70.170.21.9.php">RandomCraft</a></p></td>
            <td class="tserver">
     
    <a href="server/70.170.21.9.php"><img style="border:none;width:468px;height:60px;" src="/uniqueminecraftservers/slist/banners/1.jpg"></a>
     
    <br>IP: 70.170.21.9:25565</td>
            <td class="ttype"><p class="wrap-ttype">Mini-Game</p></td>
            <td class="tplayers"></td>
     
            <td class="tstatus Online">
    <?php
    if(!$response) {
    echo"Offline";
    } else {
    echo"Online";
    }
    ?>
     
    </td>
            </tr>
    PHP Script:
    PHP:
    <?php
     
        
    /**
        * Minecraft Server Status Query
        * @author Julian Spravil <[email protected]> https://github.com/FunnyItsElmo
        * @license Free to use but dont remove the author, license and copyright
        * @copyright © 2013 Julian Spravil
        */
        
    class MinecraftServerStatus {
            private 
    $timeout;
     
            
    /**
            * Prepares the class.
            * @param int    $timeout  default(3)
            */
            
    public function __construct($timeout 5) {
                
    $this->timeout $timeout;
            }
     
            
    /**
            * Gets the status of the target server.
            * @param string    $host    domain or ip address
            * @param int    $port    default(25565)
            */
            
    public function getStatus($host '127.0.0.1'$port 25565) {
     
                
    //Transform domain to ip address.
                
    if (substr_count($host '.') != 4$host gethostbyname($host);
     
                
    //Get timestamp for the ping
                
    $start microtime(true);
     
                
    //Connect to the server
                
    if(!$socket = @stream_socket_client('tcp://'.$host.':'.$port$errno$errstr$this->timeout)) {
     
                    
    //Server is offline
                    
    return false;
     
     
                } else {
     
                    
    stream_set_timeout($socket$this->timeout);
     
                    
    //Write and read data
                    
    fwrite($socket"\xFE\x01");
                    
    $data fread($socket2048);
                    
    fclose($socket);
                    if(
    $data == null) return false;
     
                    
    //Calculate the ping
                    
    $ping round((microtime(true)-$start)*1000);
     
                    
    //Evaluate the received data
                    
    if (substr((String)$data35) == "\x00\xa7\x00\x31\x00"){
     
                        
    $result explode("\x00"mb_convert_encoding(substr((String)$data15), 'UTF-8''UCS-2'));
                        
    $motd preg_replace("/(§.)/""",$result[1]);
     
                    }else{
     
                        
    $result explode('§'mb_convert_encoding(substr((String)$data3), 'UTF-8''UCS-2'));
     
                        
    $motd "";
                        foreach (
    $result as $key => $string) {
                            if(
    $key != sizeof($result)-&& $key != sizeof($result)-&& $key != 0) {
                                
    $motd .= '§'.$string;
                            }
                        }
     
                        
    $motd preg_replace("/(§.)/"""$motd);
     
                    }
                    
    //Remove all special characters from a string
                    
    $motd preg_replace("/[^[:alnum:][:punct:] ]/"""$motd);
     
                    
    //Set variables
                    
    $res = array();
                    
    $res['hostname'] = $host;
                    
    $res['version'] = $result[0];
                    
    $res['motd'] = $motd;
                    
    $res['players'] = $result[sizeof($result)-2];
                    
    $res['maxplayers'] = $result[sizeof($result)-1];
                    
    $res['ping'] = $ping;
     
                    
    //return obj
                    
    return $res;
                }
     
            }
        }
     
    ?>
    It shows offline all the time. I am pinging a server I KNOW is online.
     
    SpiderLink likes this.
  11. try going to the servers properties file and setting enable jquery true?

    huh LOL I'm getting the same problem too XD I set enable-JQuery to true tho?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 19, 2015
  12. Offline

    BurritoBashr

    You can't place the class last you need to save the class into a different file "query.class.php" then at the very top do
    Code:php
    1.  
    2. <?php
    3. require_once('path/query.class.php');
    4. ?>
    5.  

    Then instantiate the class and use it.
     
  13. I'm not, this is what I'm doing:
    PHP:
    <body>
     
    <?php
     
    include_once 'status.class.php'//include the class
    $status = new MCServerStatus(); // call the class
     
    $response $status->getStatus('shadowcraft.pwnz.org'); // call the function
     
    $statusTxt "offline";
    $version ="N/A";
    $ping ="N/A";
    $players "0/0";
     
    if(
    $response){
        
    $statusTxt="online";
        
    $players $response['players'] . "/" $response['maxplayers'];
        
    $version $response['version'];
        
    $ping $response['ping'];
    }
     
    ?>
    <div id="banner">
    <div id="status"><?php
    echo$statusTxt;
    ?></div>
    <div id="version"><?php
    echo$version;
    ?></div>
    <div id="ping"><?php
    echo$ping;
    ?></div>
    <div id="players"><?php
    echo$players;
    ?></div>
    </div>
     
    </body>
    inside the body of course
     
  14. Offline

    BurritoBashr

    I'm not sure, I've used a lot of classes for query lately and they've all been failing.
     
  15. Offline

    AWITFC

    Thanks, This was very useful.
     
    Last edited: Sep 20, 2015
  16. Offline

    Vidkol

    Error.....
    PHP:
    include_once 'status.class.php'//include the class
    You missed the brackets.

    Fixed.....
    PHP:
    include_once('status.class.php'); //include the class
    Brackets activated!
     
  17. Offline

    hovercatz

    This script doesnt work. No errors. I have tested this on several servers which has query enabled, with the correct port.
     
  18. Offline

    ronharsh

    Tested, works fine on my main server Windows Server 2012 w/IIS. Thanks for the script! :p

    Amended: Using IIS 8
     
  19. Offline

    mattrick

    Just a heads up, pining 1.7 servers return an array for version and players. I just use the implode() method to remove the array part.
     
  20. Offline

    Jake6177

    Parenthesis* are not needed for include, include_once, require, or require_once functions.
     
  21. Offline

    tyzoid

    include, include_once, require, etc. are not functions to begin with.

    It won't cause an error, but standard practice is to put parentheses around includes.
     
  22. Offline

    Jake6177


    Good point, they're statements. While it is standard practice, I was pointing out that they aren't required as my quoted post clearly defined.
     
  23. Offline

    McLive

  24. Offline

    TNTUP

  25. Offline

    Tendonsie

    What is the server is running Bungee?
     
  26. Offline

    TNTUP


    Tried on my bun.gee test server, query works. It must be enabled in its config to be able to query it
     
  27. Offline

    FunnyItsElmo

    Okay the status query is updated to the minecraft version 1.7.* hope all your problems are fixed. Please take a look at the new tutorial some things has changed :)
     
  28. Offline

    tenten8401

    heh heh, http://x10hosting.com has sockets enabled, and its free
     
  29. Offline

    IanSzot

    It says the server "mcctf.com" have 0/0 players online, but there's people playing D:
    [​IMG]

    Can you help me?
     
  30. Offline

    john01dav

Thread Status:
Not open for further replies.

Share This Page