Search code examples
phpstatussteam

PHP Team Fortress 2 Server Status always returns offline


I am trying to integrate a server status on my homepage, a simple image system ( one is displayed when the server is on and other when the server is off ) but the current php code is always returning offline, when the server is online:

<?php

$ip = "173.208.81.98"; 
$port = "27015"; 

$sock = @fsockopen( $ip, $port, $num, $error, 1 );

if( !$sock ){


echo( "<img src='../assets/images/off2.png' style='width: 100%'>");
}

else( $sock ){


echo( "<img src='../assets/images/on2.png' style='width: 100%'>" );
fclose($sock);

}

?>

It's a Team Fortress 2 Server, in case it matters


Solution

  • I dont know why it works, but it seems to work with stream_socket_client. Apparently not

    $ip = "173.208.81.98";
    $port = 27015;
    if(@stream_socket_client("tcp://$ip:$port") !== false) {
        echo 'online';
    } else {
        echo 'offline';
    }
    

    E:

    https://developer.valvesoftware.com/wiki/Server_queries

    there is a list of commands you can send to the server. However there is no online option. I think you still need to check if stream_socket_client is false. If it is false, the server definitly is down. It is possible, that you can reach the server but it is listed as unavailable. I don't know how Valve handles those things. I noticed that it isn't congruent to the list at firepoweredgaming.com.

    So, basically the following don't really suit you. But maybe you can use it.

        function server($ip, $port) {
            $command = "\377\377\377\377TSource Engine Query\0";
            $socket = stream_socket_client("udp://$ip:$port");
            fwrite($socket, $command);  // Send the command
            $JunkHead = fread($socket,4);
    
                fread($socket, 1); // Always equal to 0x49, Header chunk
                $return['protocol'] = ord(fread($socket, 1));
                $return['name'] = getNextString($socket);
                $return['map'] = getNextString($socket);
                $return['folder'] = getNextString($socket);
                $return['game'] = getNextString($socket);
                $return['id'] = ord(fread($socket, 2));
                $return['players'] = ord(fread($socket, 1));
                $return['max_players'] = ord(fread($socket, 1));
                $return['bots'] = ord(fread($socket, 1));
                $return['server_type'] = fread($socket, 1); // [d]edicated, [l] non-dedicated, [p] SourceTV
                $return['environment'] = fread($socket, 1); // [l]inux, [w]indows or [m]ac
                $return['visibility'] = ord(fread($socket, 1)); // 1==private
                $return['vac'] = ord(fread($socket, 1)); // 1==secure
                $return['version'] = getNextString($socket);
    
            return $return;
        }
    
        /**
         * freads until \0 is found. Nullterminated string.
        */
        function getNextString($socket) {
            $str = '';
            while(($read = fread($socket, 1)) != "\0") {
                $str .= $read;
            }
            return $str;
        }
    

    I have a function called server which reads data into an array. (You can print it with print_r(server('173.208.81.98', 27015))). I used ord() to get numbers out of the ascii, because php reads the socket as ascii.