Advertisement
Flawedspirit

Minecraft Server Query Interface

Feb 27th, 2014
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.35 KB | None | 0 0
  1. <?php
  2.     // Display errors on-page for those who don't want to (or can't) view their error.log
  3.     // It can be safely removed
  4.     Error_Reporting( E_ALL | E_STRICT );
  5.     Ini_Set( 'display_errors', true );
  6.  
  7.     require "ServerQuery.class.php";
  8.  
  9.     $timer = microtime(true);
  10.     $query = new ServerQuery();
  11.  
  12.     try {
  13.         $query -> connect(SERVER_ADDR, SERVER_PORT, TIMEOUT);
  14.     } catch(ServerQueryException $ex) {
  15.         $exception = $ex;
  16.     }
  17.  
  18.     // Display script runtime in milliseconds
  19.     $timer = number_format((microtime(true) - $timer) * 1000, 0, '.', '');
  20.  
  21.     // To display runtime in seconds instead, uncomment next line and comment line 19
  22.     //$timer = number_format((microtime(true) - $timer), 2, '.', '');
  23. ?>
  24.  
  25. <?php if(isset($exception)): ?>
  26.     <p><?php echo htmlspecialchars($exception -> getMessage()); ?></p>
  27. <?php else: ?>
  28.     <table width="100%">
  29.         <thead><tr><th colspan=2>Server Info <em>(queried in <?php echo $timer; ?>ms)</em></th></tr></thead>
  30.         <tbody>
  31.             <?php if(($info = $query -> getServerInfo()) !== false): ?>
  32.                 <?php foreach($info as $infoKey => $infoValue): ?>
  33.                     <tr><td><?php echo "<strong>" . htmlspecialchars($infoKey) . "</strong>"; ?></td>
  34.                     <td><?php
  35.                         if(is_array($infoValue)) {
  36.                             echo "<pre>";
  37.                             print_r($infoValue);
  38.                             echo "</pre>";
  39.                         } else {
  40.                             echo htmlspecialchars($infoValue);
  41.                         } ?>
  42.                     </td></tr>
  43.                 <?php endforeach; ?>
  44.             <?php else: ?>
  45.                 <tr><td colspan=2>No data received.</td></tr>
  46.             <?php endif; ?>
  47.         </tbody>
  48.     </table>
  49.  
  50.     <table width="100%">
  51.         <thead>
  52.             <tr><th>Players Online</th></tr>
  53.         </thead>
  54.         <tbody>
  55.             <?php if(($players = $query -> getPlayerList()) !== false): ?>
  56.                 <?php foreach($players as $player): ?>
  57.                     <tr><td><?php echo htmlspecialchars($player); ?></td></tr>
  58.                 <?php endforeach; ?>
  59.                 <?php else: ?>
  60.                     <tr><td><em>There is no one online.</em></td></tr>
  61.             <?php endif; ?>
  62.         </tbody>
  63.     </table>
  64. <?php endif; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement