Hack98.Check Your Network Game with PHP


Hack 98. Check Your Network Game with PHP

Use the Net_GameServerQuery PEAR module to check up on your network game, all using PHP.

There is a PEAR module [Hack #2] for almost anything, it seems! Proving that to be true, this hack uses the Net_GameServerQuery module to check up on a Half Life server, just to see how many people are playing (and to show off PEAR and yet another cool module).

10.5.1. The Code

Save the code in Example 10-9 as index.php.

Example 10-9. Checking the Half Life server as an automated task
 <?php require( 'Net/GameServerQuery.php' ); $protocol = 'halflife'; $ip = '66.159.222.15'; $gsq = new Net_GameServerQuery();  $gsq->addServer( $protocol, $ip ); $res = $gsq->execute(); ?> <html> <head> <title>Game Server Status</title> </head> <style> body { font-family: arial, verdana, sans-serif; } th { font-size: xx-small; border-bottom: 1px solid black; } td { font-size: xx-small; vertical-align: top; } .num-players { text-align: center; } .header { font-weight: bold; } </style> <body> <table> <tr> <td >Protocol</td> <td><?php echo($protocol); ?></td> </tr> <tr> <td >IP</td> <td><?php echo($ip); ?></td> </tr> </table> <table width="100%" cellspacing="0" cellpadding="3"> <tr> <th width="20%">IP/Port</th> <th width="20%">Password</th> <th width="20%">Hostname</th> <th width="20%">Players</th> <th width="20%">Mod</th> </tr> <?php foreach( $res[0] as $r ) { ?> <tr> <td width="20%"><?php echo($r['ip']); ?><br/><?php echo($r['port']); ?></td> <td width="20%"><?php echo($r['password']); ?></td> <td width="20%"><?php echo($r['hostname']); ?></td> <td width="20%" > <?php echo($r['numplayers']); ?> current<br/> <?php echo($r['maxplayers']); ?> max </td> <td width="20%"><?php echo($r['mod']); ?></td> </tr> <?php } ?> </table> </body> </html> 

10.5.2. Running the Hack

First, install the Net_GameServerQuery PEAR module on your PHP installation [Hack #2]. Next, change the $ip and $protocol variables to match your game server's IP address and game type.

The Net_GameServerQuery module's documentation (http://pear.php.net/net_gameserverquery) includes the list of protocols in their package information.


Upload the script to the PHP server and surf to the page using your web browser. You should see something like Figure 10-14.

Figure 10-14. The Half Life game server status


You can use data such as this in several ways. You can create a small WML page for your phone to check the games, and you can even build a small page for your PSP. That way, you can know right away when there is a game worth playing! If you're responsible for keeping the server running, you can poll it and restart the server if the game isn't running. That's a lot better than waking up in the middle of the night, isn't it?

10.5.3. See Also

  • "Read RSS Feeds on Your PSP" [Hack #90]



PHP Hacks
PHP Hacks: Tips & Tools For Creating Dynamic Websites
ISBN: 0596101392
EAN: 2147483647
Year: 2006
Pages: 163

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net