Serverstatus Script

It displays some basic information about a multiplayer NWN server. Providing it is visible over the internet, then there are no extra requirements (ie no database or special router settings) needed. The script needs to know the address of the server, so its address needs to be fixed, either by using a fixed IP address or a DNS service (we use dyndns). I decided to stick the script here after this thread on the BioBoards, which is why the code uses that server’s address.

Example output:

Server Address: legacyofnetheril.servegame.org:5121
Server Status: Online
Players: 3 / 24

These variables may need altering:

  • $serveraddr – should be the fixed IP address or the URL used to connect to the server
  • $server_down – the message that gets displayed if the script can’t find the server
  • $port – if the server is running on the default port 5121, then you do not need to do anything. If it is running on a different port you need to add a line below $serveraddr:
    $port="5122" (replace it with the port number you use), then change the $status … line to read
    $status = nwserver_status($serveraddr, $port);

The PHP code

<?php
 
# function: nwserver_status
# Returns the server status in an array
# If the server does not respond with the expected hex string,
# it returns an error message (which is not an array).

 
function nwserver_status($serveraddr, $port="5121", $timeout=5) {
	$connect = fsockopen( "udp://" . $serveraddr, $port, $errno, $errstr, $timeout );
	$error = NULL;
	if ( ! $connect )    {
	    $error = "server down";
	    return $error;
	} else {
	    socket_set_timeout( $connect, $timeout );
	    $send = "\\xFE\\xFD\\x00\\xE0\\xEB\\x2D\\x0E\\x14\\x01\\x0B\\x01\\x05\\x08\\x0A\\x33\\x34\\x35\\x13\\x04\\x36\\x37\\x38\\x39\\x14\\x3A\\x3B\\x3C\\x3D\\x00\\x00";
	    fwrite( $connect, $send );
	    $output = fread( $connect, 5000 );
	    if ( ! $output ) {
            $error = "no reply";
	    } else {
            $statusarray = explode( "\\x00", $output );
            if (count($statusarray) < 6) { // if the array has < 6 elements, we're referencing a non-existing element
                $error = "bad reply";
            }
	    }
	}
	fclose( $connect );
	if ($error == NULL) {
		return $statusarray;
	} else {
		return $error;
	}
}
 
# php script to get the server status and player list

// gets server status directly from the gameserver
 
$serveraddr = "legacyofnetheril.servegame.org";
// $server_down is displayed if the script can't connect 
// or if it receives no data from the server
$server_down = "<p class=\\"highlight\\">The server is down - now what are you gonna do?</p>\\n";
 
 
$status = nwserver_status($serveraddr);
if (!is_array($status)) { // if an error message was returned
	print ($status);
} else {
    // format serverstatus array
	// $status[11] is the port used, $status[5] the number of players online
	// $status[6] the maximum number of players allowed
    print( "Server Address: <strong>$serveraddr:$status[11]</strong><br />\\n" );
    print( "Server Status: <strong>Online</strong><br />\\n" );
    print( "Players: <strong>$status[5]</strong> / <strong>$status[6]</strong><br />\\n" );
}
 
?>
 
 
 

Download code: nwserver_status.php.txt

Rename it to nwserver_status.php. The parsed PHP script to show it works.


Comments

  1. Phoenix said June 6, 2006, 9:25 am:

    How would one alter this for passworded servers?

  2. fluffyamoeba said June 6, 2006, 3:38 pm:

    It should work unchanged for a passworded server. Our server has a password and uses the script.

  3. I am new to the php boards. Where do I place this at?

  4. fluffyamoeba said June 19, 2006, 1:14 am:

    It will work as a standalone script. However, if you want to integrate it into a CMS/forum, then you would be best asking in the support forums for the software you use. I can explain how to do it for vBulletin as that is what we use for our forums, but not for anything else.

  5. This is just what we need on our page. I am not a strong scripter, what variables would i have to change to get this to work. I am getting the following error

    Parse error: syntax error, unexpected T_STRING, expecting ‘)’ in /home/altharia/public_html/nwserver_status.php.txt on line 9

    As I said I am worthless with this stuff. Any help I can get would be greatly appreciated.

  6. I actually got this to sort of work. It gives me the no reply error so am looking for the proper line to take out. I assume it is the $connect but am not sure

  7. fluffyamoeba said July 4, 2006, 11:51 am:

    The only things you might need to change would be the port number and the server address. Everything else will work for any NWN server, providing you can see it in GameSpy.

    Try double checking that you didn’t edit the hex string the script sends to the server as that has to be exactly the same for it to reply.

  8. Much thanx to fluffyamoeba for helping me with this. The script works fine on another site. Our problem is that our host charges extra for a dedicated ip. With our current package only port 80 is open for such scripts. Wanted to pass this info on.

  9. fluffyamoeba said August 2, 2006, 3:50 pm:

    It looks like since my host migrated the site to a new server, the port the script uses is blocked, so the example won’t work anymore here. Some people who had this problem have been able to get the port opened on asking their host, so it’s always worth a try.

    As this sort of thing doesn’t tend to be mentioned in the host’s package overview, it might be worth anyone using my script or other variations of it mentioning which hosts it works on, which will open the port on request and which aren’t quite so helpful.