Im building a whole damn website built around my samp server. Heres the function i wrote today when i was making my live rcon line.
Code:
define("game_server_host","IP");
define("game_server_port",PORT);
//^^ Just make sure the command can read this unless you change them. I have them defined because i use them across my whole website
function GetGTAServerStatus($image,$hostname,$port)
{
$realtimeout = ini_get("default_socket_timeout");
ini_set("default_socket_timeout","1");
if(!isset($image)) $image = 1;
if(!isset($hostname)) $fp = fsockopen("udp://".game_server_host, game_server_port, $errno, $errstr);
else if(isset($hostname) && !isset($port)) $fp = fsockopen("udp://".$hostname, 7777, $errno, $errstr);
else if(isset($hostname) && isset($port)) $fp = fsockopen("udp://".$hostname, $port, $errno, $errstr);
ini_set("default_socket_timeout",$realtimeout);
if($fp)
{
$packet = 'SAMP';
$packet .= chr(strtok($ip, '.')).chr(strtok('.')).chr(strtok('.')).chr(strtok('.'));
$packet .= chr($port & 0xFF);
$packet .= chr($port >> 8 & 0xFF);
fwrite($fp, $packet.'i');
fread($fp, 11);
$is_passworded = ord(fread($fp, 1));
$plr_count = ord(fread($fp, 2));
$max_plrs = ord(fread($fp, 2));
$strlen = ord(fread($fp, 4));
$hostname = fread($fp, $strlen);
fclose($fp);
if(strlen($hostname) > 0)
{
if($image) echo("<center><a href=\"status.php\"><img src=\"images/serverup.png\" alt=\"The Game Server Is Up!\" border=\"0\"></a></center>");
return 1;
}
if(strlen($hostname) == 0)
{
if($image) echo("<center><img src=\"images/serverdown.png\" alt=\"The Game Server Is Down!\"></center>");
return 0;
}
}
if($image) echo("<center><img src=\"images/serverdown.png\" alt=\"The Game Server Is Down!\"></center>");
return 0;
}
Syntax: GetGTAServerStatus($image,$hostname,$port);
$image = Integer, 1 or 0. Declares whether or not to echo the image(serverup/serverdown).
$hostname = if not defined, uses the declared game_server_host.
$port = if not defined, uses the declared game_server_port;
if $hostname is defined, but port is not, it uses port 7777.
I hope you enjoy it. When you first start your server, give it 30 seconds or so to get the socket running.
EDIT: the reason for the if(strlen($hostname) == 0) is because sometimes the socket still connects for some strange reason but it wont be able to get the variables. with that it does a double check to see if its connected.
The reason i set the ini to 1, is because for some reason it takes me for effin ever to get a no connection error without it. even when i put 1, it doesnt work.