Jump to content
  • 0

Get online players [PHP]


Madykevy

Question

12 answers to this question

Recommended Posts

  • 0

No clue where I snatched it from some time ago, but you can just use:

<?php
// Don't want direct calling of this file
if(count(get_included_files()) == 1) exit("Go away!");
function strToHex($string)
{
    $hex='';
    for ($i=0; $i < strlen($string); $i++)
    {
        $hex .= dechex(ord($string[$i]));
    }
    return $hex;
}

function hexToStr($hex)
{
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2)
    {
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}

function computeUnsignedCRC32($str){
   sscanf(crc32($str), "%u", $var);
   $var = dechex($var + 0);
   return $var;
}
 
function dec_to_hex($dec)
{
    $sign = ""; // suppress errors
	$h = null;
    if( $dec < 0){ $sign = "-"; $dec = abs($dec); }

    $hex = Array( 0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5,
                  6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 'a',
                  11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e',   
                  15 => 'f' );
       
    do
    {
        $h = $hex[($dec%16)] . $h;
        $dec /= 16;
    }
    while( $dec >= 1 );
   
    return $sign . $h;
} 

function get_checksum($cs)
{
    $var = computeUnsignedCRC32($cs);
	//echo "crchex: ".$var."<br/>";
	$x = ('0x');
	$a = substr($var, 0, 2);
	$a = $x.$a;
	$b = substr($var, 2, 2);
	$b = $x.$b;
	$c = substr($var, 4, 2);
	$c = $x.$c;
	$d = substr($var, 6, 2);
	$d = $x.$d;
	return chr($d).chr($c).chr($b).chr($a);
} 

function rcon($serverip,$serverport,$rconpassword,$cmd){
	$passhead = chr(0xFF).chr(0x00);
	$head = chr(0x42).chr(0x45);
	$pass = $passhead.$rconpassword;
	$answer = "";
	$checksum = get_checksum($pass);

	$loginmsg = $head.$checksum.$pass;

	$rcon = fsockopen("udp://".$serverip, $serverport, $errno, $errstr, 1);
	stream_set_timeout($rcon, 1);

	if (!$rcon) {
		echo "ERROR: $errno - $errstr<br />\n";
	} else {
		fwrite($rcon, $loginmsg);
		$res = fread($rcon, 16);
		
		$cmdhead = chr(0xFF).chr(0x01).chr(0x00);
		//$cmd = "Players";
		$cmd = $cmdhead.$cmd;
		$checksum = get_checksum($cmd);
		$cmdmsg = $head.$checksum.$cmd;
		$hlen = strlen($head.$checksum.chr(0xFF).chr(0x01));
		
		fwrite($rcon, $cmdmsg);
		$answer = fread($rcon, 102400);
		
		if ( strToHex(substr($answer, 9, 1)) == "0"){
			$count = strToHex(substr($answer, 10, 1));
			//echo $count."<br/>";
			for ($i = 0; $i < $count-1; $i++){
				$answer .= fread($rcon, 102400);
			}
		}
		//echo strToHex(substr($answer, 0, 16))."<br/>";
		//echo strToHex($answer)."<br/>";
		//echo $answer."<br/>";
		$cmd = "Exit";
		$cmd = $cmdhead.$cmd;
		$checksum = get_checksum($cmd);
		$cmdmsg = $head.$checksum.$cmd;
		fwrite($rcon, $cmdmsg);
	}

	return $answer;
}
?>

Save it as rcon.php and then use it in your site like:

include("rcon.php");
$reply = rcon('IP',PORT,'RCONPASS','COMMAND');
// For example
// rcon('127.0.0.1', 2302, 'changeme', 'players');
print_r($reply);

Before printing, be sure to filter the answers / format it etc.. but you can basicly do everything RCON can, that way.. verification-checks, playerlists, kicks/bans/restarts/younameit

 

Cheers,

DNightmare

Link to comment
Share on other sites

  • 0

How do you filter out just to show names and total online in column like this

 

1# Player Name 1

2# Player Name 2

3# Player Name 3

4# Player Name 4

5# Player Name 5

Online 5/40

 

 

as it is now i do get like this

BE[Players on server: [#] [IP Address]:[Port] [Ping] [GUID] [Name] -------------------------------------------------- 0 127.0.0.1:2316 -1 932ba042360e6e49f67e7aa5def48b84(?) HC-BOT 1 7.83.35.122:2304 125 30aadc0fd8b69d6a09652704188057bd(?) Donavan (2 players in total)
Link to comment
Share on other sites

  • 0

bump, is this still working ? cause its not working for me....has anything changed since September ?

I have fsockets on in my php.ini....but i just get a white page.

There used to be a script (if i remember correctly) that didnt require the rcon pass to show the players. Was it based on Gamespy responds ? cant remember.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Discord

×
×
  • Create New...