[bf1942] BF2 Stats

Jorrit Schippers jorrit at gameparty.net
Tue Jul 5 12:25:55 EDT 2005

Perhaps this piece of code gets you anywhere. Be careful that the port parameter is the gamespy port, not the game port. It uses the newest Gamespy protocol as far as I know, at least the one with the 0x0 byte separator, not the slash separator.

People with knowledge of PHP: feel free to comment.

function getplayerlist($ip, $port = 29900, &$errormsg, $timeout = 10) {
	$initgarbagelength = 7;
	$partsep = chr(0).chr(0);
	$fieldsep = chr(0);
	$fp = fsockopen('udp://'.$ip, $port, $errno, $errstr, $timeout);
	if (!$fp) {
		$errormsg = 'Error connecting to '.$ip.':'.$port.': #'.$errno.': '.$errstr;
		return false;
	socket_set_timeout($fp, $timeout);
	fwrite($fp, hexchar('fefd002121212100ff00'));
	$data = fread($fp, 10000);
	$data = substr($data, $initgarbagelength, strlen($data));
	$parts = explode($partsep, $data);
	$players = array();
	$fields = explode($fieldsep, $parts[0]);
	foreach($fields AS $k=>$v) {
		$fields[$k] = substr($v, 0, strlen($v) - 1);
	$playerdata = explode($fieldsep, $parts[1]);
	$numfields = count($fields);
	$numplayers = count($playerdata) / $numfields;
	for($i=0;$i<$numplayers;$i++) {
		for($j=0;$j<$numfields;$j++) {
			$players[$i][$fields[$j]] = $playerdata[$i*$numfields+$j];
	usort($players, 'playerlistcompare');
	return $players;

function playerlistcompare($a, $b) {
	return strcmp($a['player'], $b['player']);

function hexchar($input) {
	$chars = array();
	for($i = 0; $i < strlen($input); $i+=2) {
		$chars[] = chr(hexdec($input{$i}.$input{$i+1}));
	return implode('', $chars);

Jorrit Schippers

-----Oorspronkelijk bericht-----
Van: Rüdiger [mailto:Webmaster at robin1977.de] 
Verzonden: dinsdag 5 juli 2005 10:14
Aan: bf1942 at icculus.org
Onderwerp: [bf1942] BF2 Stats

I think you misunderstood my question last time. I would like to know if there is a complete script to display the stats of an unranked server on a Homepage. Not the playerstats of ranked servers. I am not a good PHP-coder, so its a unsolvable problem for me to code such a script. Can anyone help me with a complete stats script for listing players who played on the local unranked BF2-server?

More information about the Bf1942 mailing list