Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Anything wrong with this server status grabber?

    Do you see anyhing wrong with this?

    http://snipt.org/AZgf2

    Code:
    <?php
    
        /**
         * Minecraft Server Status Query
         * @author Julian Spravil <julian.spr@t-online.de> 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 = 3) {
                $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 = '95.34.153.91', $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($socket, 2048);
                    fclose($socket);
                    if($data == null) return false;
    
                    //Calculate the ping
                    $ping = round((microtime(true)-$start)*1000);
    
                    //Evaluate the received data
                    if (substr((String)$data, 3, 5) == "\x00\xa7\x00\x31\x00"){
    
                        $result = explode("\x00", mb_convert_encoding(substr((String)$data, 15), 'UTF-8', 'UCS-2'));
                        $motd = preg_replace("/(.)/", "",$result[1]);
    
                    }else{
    
                        $result = explode('', mb_convert_encoding(substr((String)$data, 3), 'UTF-8', 'UCS-2'));
    
                        $motd = "";
                        foreach ($result as $key => $string) {
                            if($key != sizeof($result)-1 && $key != sizeof($result)-2 && $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;
                }
    
            }
        }
    
    
    
    
    
    
    
    	$status = new MinecraftServerStatus();
    	$response = $status->getStatus('95.34.153.91', 25565); // when you dont have the default port 
    	if(!$response) {
    		echo"The Server is offline!";
    	} else {
    		echo"The Server ".$response['hostname']." is running on ".$response['version']." and is online,
    		currently are ".$response['players']." players online
    		of a maximum of ".$response['maxplayers'].". The motd of the server is '".$response['motd']."'. 
    		The server has a ping of ".$response['ping']." milliseconds.";
    	}
    
    ?>

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,091
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by petterhanssen View Post
    Do you see anyhing wrong with this?
    Should we?

    If you're experiencing problems, it would be wise to tell us what is wrong rather than leave us guessing what we're supposed to solve for you.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #3
    New Coder
    Join Date
    Aug 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sorry

    Quote Originally Posted by tangoforce View Post
    Should we?

    If you're experiencing problems, it would be wise to tell us what is wrong rather than leave us guessing what we're supposed to solve for you.
    Yes, sorry I didnt think about it, I dont know why.

    The problem is that it shows that my minecraft server is offline, but i know it is online.

    I can connect to it with an external IP, and other public server grabber tools show the server as online.

    IP: mc.pridenetwork.net
    Port: 25565 / DEFAULT


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •