Jump to content

PHP script that can Query MTA Servers.


Recommended Posts

I made this script just for fun.

It makes possible to Query ESA enabled servers.

You directly wonder why?

It's usefull if you want give information of your own server on your homepage.

I didnt got out the bug where the server name and Player not show correctly when there are less then 10 players on the server.

You can test it here:

http://dutchtux.decksix.com/mtaquery/query.php

And the basic source is here:

               // Caching
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
                                                    // always modified
header("Cache-Control: no-store, no-cache, must-revalidate");  // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");                          // HTTP/1.0

// Getting information of teh form.
$ServerIP = ""// Server IP here
$ServerPort = "" // Server IP here
$fp = fsockopen("udp://" . $ServerIP, $ServerPort + 123, $errno, $errstr);

if (!$fp) {
    echo "$errstr ($errno)
\n";
    exit;
} else {	
    fputs ($fp, "s");	
    $c = '';
    do {
	    $c .= fread ($fp,1); 
	    $fpstatus = socket_get_status($fp);  
 	   } while ($fpstatus["unread_bytes"] ); 
   fclose ($fp);

   $playerquery = strchr($c,"?");
   $serverquery = substr($c,0, strlen($c) - strlen($playerquery));

   $players = explode ("?",$playerquery);
   unset($players[0]); 	// Remove Array number 0 because nothing is entered by explode.

	$serverquery = str_replace("","<=>",$serverquery); 
	$serverquery = str_replace("","<=>",$serverquery);
	$serverquery = str_replace("","<=>",$serverquery);  
	$serverquery = str_replace("","<=>",$serverquery);  
	$serverquery = str_replace("","<=>",$serverquery);  
	$serverquery = str_replace("","<=>",$serverquery);

    for($i=0;$i<(count($serverquery));$i++){

        $explodedata = explode("<=>",$serverquery);

 	    }
       }

	$string = "$ServerPort";
	$stringlengte = strlen($string);

	$server_name_ip = $ServerPort; 
	$server_name_ip .= $explodedata[2][$stringlengte]; 

	$server_name = str_replace("$server_name_ip","",$explodedata[2]); 

if (!$serverquery) {
	echo "Server offline";
} else {
	echo $server_name;  // Server name
	echo $explodedata[3]; // Game name
	echo $explodedata[4]; // version
	echo $explodedata[6] ."/" . $explodedata[7]; // Current Players and max players

	// Player List
	$pcount = 0;
	foreach (array_keys($players) as $player) { 
		$pcount++;	
		echo $pcount; // Player number
		echo  substr($players[$player],1,strlen($players[$player]) - 6); // Player name
	}
}

Edited by Guest
Link to comment

Someone has fixed the bugs thats found in the script.

I updated the basic script from above.

Thank again for fixing those Mulle.

You can use this code freely.

If the basic script doesnt work....Just send me E-mail and you will get the source of the sample script that is on my site.

Link to comment
  • 1 month later...

<?=$simp_sw[6]?>


<?=$simp_sw[6]?> is a multiplayer mod for <?=$simp_sw[7]?> for the PC. You can get it from http://www.MTAVC.com/. It's still in its developing stages but its well worth the download. I have a dedicated linux server on a 10 Mbit network.


Name: <?=$servername ?>
IP and Port: mta.robpol86.com:<?=$port_num ?>
Users: <?=$user_count ?>
Status: <?=$srvstatus ?>


<?php
if ($srvstatus_code == 2 || $srvstatus_code == 3) //decide to show user table or not
{
echo "Current players in server:
";
echo "
";
?>
Raw Data from Server: <?=$c?>

<? }?>
Link to comment

if the good people behind mta could work this into there offical server list page, like have alink to unoffical servers? which could list them all from the mta website, it would make mta a lot more popular, if the offical servers are full i can't be bothered to download ase, i know its free and you can keep clicking try for 7 more days but i'd still prefer to use this script if it works properly :)

Link to comment
here, thnx 2 DutchTux i got a good jump start w/ my server stats, and i resumed work on it since .2 came out.

to test it, go here

http://www.robpol86.com/?id=mta

and click on the title to toggle between servers.

this bitch does MTA:VC 0.2 AND GTA3:MTA!

and heres the source if ne1 cares


lol, good luck understanding that! somehow i got all that bs to work! dont just change the ip and port and expect it to work, cuz it WONT! you have to modify this. this is just a "guide". and dont go asking me to do it for u!

any tips on making this simpler or making it do more stuff "like get KILLS and DEATHS from server sintax and ijs will be greatly appreciated :wink: !!

Is this code still affected by the base <10 Player Issue? I got the tracking working fine for a bigger then 10 player, but I only host a 5 player, and the script does not like that :(

P.S. Here is the Best version of the stats I found, Looks to be hosted from here. Any input is apprecated for my noob coding self :)

Link to comment
  • 2 weeks later...
  • 2 weeks later...

<?
$server['ip'] = $_GET['ip'];
$server['port'] = $_GET['port'];

$fp = @fsockopen("udp://" . $server['ip'], $server['port'] + 123);

if(!$fp) {
 die("No Response");
} else {
 fwrite($fp,"s");
 $data = fread($fp,4096);
 $data = preg_replace("/{5}\??|\?/","/////",$data);
 $data = explode("/////",$data);

 $raw = preg_replace("/^EYE1??(gta3)?mta\[0-9]{4}||||/","/////",$data[0]);
 list($null,$server['name'],$server['version'],$null,$server['current'],$server['max']) = explode("/////",$raw);

 $server['players'] = array_slice($data,1,-1);
}

print_r($server);
?>

to display the players correctly you'd just chop the first character off like

foreach($server['players'] as $temp) {
 echo substr($temp,1) . "
";
}

should work for gta3 and vc

Link to comment

? wtf? weird, i dont get "/////"

EYE1mta2003Robpol86's MTA:VC v0.2 ServerMTA Vice City0.2.00515  ?Fekete? [VCT].Phuk?Ash? VC=RENAGADE?MOBAXX

i chop players into their own vars:

$user = explode("?", $c);
0 = dontworry,its_bs
1 = Fekete
2 = [VCT].Phuk
3 = Ash
4 = VC=RENAGADE
5 = MOBAXX

then i kill those gay boxes

function charfix($usr="") //fix user colors
{
$charset = ",,,,,,,	,,,,,›,,¤,©,¥,”,¡,¶,æ,ì,í,Ç,áçº,Ï,¬";
$charset = explode(",", $charset);
$a = 26;
for ($i = 0; $i <= $a; $i++)
{
	$usr = str_replace($charset[$i], "", $usr);
}
return $usr;
}
$user[1] = charfix($user[1]);

i call a function because it does more than just that, it also "fixes" names w/ colors and actually displays the right color, but im keeping that to myself. i end up outputting "Fekete" at the end. i throw this in a loop so i can list the players.

Link to comment

function color($name) {
 $colors = array("ÿÿ"=>"#FF0000");
 foreach($colors as $key => $val) {
   $name   = ereg_replace($key,"",$name);
 }
 return $name;
}

i don't really know the color codes so i only did the red one, but that should color names using:

foreach($server['players'] as $temp) {
 echo substr(color($temp),1) . "
";
}

the substr() gets rid of the first character (those gay boxes) so i don't use another function for that.. unless they show up some place else?

the final code was:

<?
function color($name) {
 $colors = array("ÿÿ"=>"#FF0000");
 foreach($colors as $key => $val) {
   $name   = ereg_replace($key,"",$name);
 }
 return $name;
}

$server['ip']   = $_GET['ip'];
$server['port'] = $_GET['port'];

$fp = @fsockopen("udp://" . $server['ip'], $server['port'] + 123);

if(!$fp) {
 die("No Response");
} else {
 fwrite($fp,"s");
 $data = fread($fp,4096);
 $data = preg_replace("/{5}\??|\?/","/////",$data);
 $data = explode("/////",$data);

 $raw = preg_replace("/^EYE1??(gta3)?mta\[0-9]{4}||||/","/////",$data[0]);
 list($null,$server['name'],$server['version'],$null,$server['current'],$server['max']) = explode("/////",$raw);

 $server['players'] = array_slice($data,1,-1);
}

print_r($server);

foreach($server['players'] as $temp) {
 echo substr(color($temp),1) . "
";
}
?>

the output would be something like:

Array
(
   [ip] => tyyy.org
   [port] => 2003
   [max] => 14
   [current] => 1
   [version] => MTA
Vice City0.2.0
   [name] => dusty's mta:vc 0.2 ded server
   [players] => Array
       (
           [0] => ÿÿtest
       )

)

test

and can be called through filename.php?ip=0.0.0.0&port=0000

and the /////'s just replace some of the separators in the raw data so i can explode by 1 delimiter

Link to comment

hmm, ur code seems a bit diff than mine, but then again i dont know all the good functions in my head. a few weeks ago i didnt even know bout strstr() or stristr()!!

heres my example of fixing colors

$combo[0] = "FF6633@@@3fÿÿ"; //orange

$combo[1] = "FF6139@@@9aÿÿ"; //orange

$a = 1;

for ($i = 0; $i <= $a; $i++)

{

list($hex, $chars) = explode("@@@", $combo[$i]);

if (stristr($usr, $chars))

{

$usrcolor = explode($chars, $usr);

$usr = $usrcolor[0] . "" . $usrcolor[1] . "" . $usrcolor[2];

}

}

i stick that in that function i made in the prv post, under the current code

i do this incase sum1 has 2 colors in their nick

like Robpol86

(damn winme and its non-able-to-paste-tabs)

Link to comment

<?
function format_player($player) {
 $player = htmlentities(substr($player,1));
 list($name,$score,$ping) = split("||||",$player);
 $colors = array("ÿÿ"=>"#FF0000");

 foreach($colors as $key => $val) {
   $name = ereg_replace($key,"",$name);
 }

 $player = array("score"=>$score,"name"=>$name,"ping"=>$ping);

 return $player;
}

$server['ip']   = $_GET['ip'];
$server['port'] = $_GET['port'];

$fp = @fsockopen("udp://" . $server['ip'], $server['port'] + 123);

if(!$fp) {
 die("No Response");
} else {
 fwrite($fp,"s");
 $data = fread($fp,4096);
 $data = preg_replace("/{5}\??|\?/","/////",$data);
 $data = explode("/////",$data);

 $raw = preg_replace("/^EYE1??(gta3)?mta\" . $server['port'] . "||||/","/////",$data[0]);
 list($null,$server['name'],$server['version'],$null,$server['current'],$server['max']) = explode("/////",$raw);

 $server['players'] = array_slice($data,1);
}

echo "";

print_r($server);

echo "\n\nActual Player Output:\n";

for($i=0;$i<$server['current'];$i++) {
 $temp = format_player($server['players'][$i]);
 print_r($temp);
 echo "\n";
}

echo "";
?>

updated to work with 0.2.2 which now shows score/ping!

Link to comment
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...