Simple server query


<?php

$ip = '88.150.159.136';
$queryport = 29416;

$socket = @fsockopen("udp://".$ip, $queryport , $errno, $errstr, 1);

stream_set_timeout($socket, 1);
stream_set_blocking($socket, TRUE);
fwrite($socket, "\xFF\xFF\xFF\xFF\x54Source Engine Query\x00");
$response = fread($socket, 4096);
@fclose($socket);

$packet = explode("\x00", substr($response, 6), 5);
$server = array();

$server['name'] = $packet[0];
$server['map'] = $packet[1];
$server['game'] = $packet[2];
$server['description'] = $packet[3];
$inner = $packet[4];
$server['players']     = ord(substr($inner, 2, 1));
$server['playersmax']  = ord(substr($inner, 3, 1));
$server['password']    = ord(substr($inner, 7, 1));
$server['vac']         = ord(substr($inner, 8, 1));

var_dump( $server );

?>

I saw many people asking on how to query a server for their website so here you go. I’ll post a RCon one shortly.

Always shows 0’s. Or my query messed up… :s

You have to use the query port, not the regular play port.

Thankyou I will be using this

Your welcome, have fun!
I can also release a RCon snippet but I need a server to test it on first.

Can I change query port somehow?

Whatever your host assigned to your server, open a ticket and ask them for the query port.

And if I host on private server (VDS)?

I’m guessing you’re using leaked files so I ain’t gonna help you, sorry.

Not really :smile:

To clear it up and avoid further useless posts, you need a query port to use this. I don’t host rust servers therefore I don’t know where to find yours without a host, I’m sorry.

array(8) { [“name”]=> string(0) “” [“map”]=> NULL [“game”]=> NULL [“description”]=> NULL [“players”]=> int(0) [“playersmax”]=> int(0) [“password”]=> int(0) [“vac”]=> int(0) }

Not sure if this is still being updated?

I’m using my query port.

change


var_dump( $server );

to



print_r( $server );


Will be easier to read and is working for me, make sure its the query port, not the port of the server & Make sure your web host allows php sockets (fsockopen and udp sockets)


Can also Print it with Json, syntax json.php?ip=123.123.123.123&port=69



<?php
$ip = $_GET["ip"];
$queryport = $_GET["queryport"];

$socket = @fsockopen("udp://".$ip, $queryport , $errno, $errstr, 1);

stream_set_timeout($socket, 1);
stream_set_blocking($socket, TRUE);
fwrite($socket, "\xFF\xFF\xFF\xFF\x54Source Engine Query\x00");
$response = fread($socket, 4096);
@fclose($socket);

$packet = explode("\x00", substr($response, 6), 5);
$server = array();

$server['name'] = $packet[0];
$server['map'] = $packet[1];
$server['game'] = $packet[2];
$server['description'] = $packet[3];
$inner = $packet[4];
$server['players']     = ord(substr($inner, 2, 1));
$server['playersmax']  = ord(substr($inner, 3, 1));
$server['password']    = ord(substr($inner, 7, 1));
$server['vac']         = ord(substr($inner, 8, 1));

echo json_encode($server);

?>


Then you can make a cool thing like this…



<?php
//GET URL json.php?ip=123.123.123.123&queryport=69 (STEAM QUERY PORT)
$ip = $_GET["ip"];
$port = $_GET["port"];

//json shtuff
$json_array = json_decode(file_get_contents("http://yourdomain.com/json.php?ip=". $ip ."&queryport=". $port), true);
$name = $json_array['name'];
$players = $json_array['players'];
$playersmax = $json_array['playersmax'];

//creating the image
$im = imagecreate(350, 20);
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 0);

//printing text
imagestring($im, 40, 5, 5, $name, $textcolor);
imagestring($im, 40, 185, 5, '| Players:', $textcolor);
imagestring($im, 40, 285, 5, "{$players} / {$playersmax}", $textcolor);

//rest of image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>


which makes something like this…

http://www.thezombieinfection.com/image.php?ip=81.19.212.45&port=2501