SA-MP Forums

Go Back   SA-MP Forums > SA-MP Server > Server Support

Reply
 
Thread Tools Display Modes
Old 29/09/2009, 10:18 AM   #1
Woet
Gangsta
 
Join Date: Oct 2006
Posts: 518
Reputation: 18
Default PHP Query (show your server on your website)

This code is originally made by Peter, as shown in the credits.
I've changed some small bits to make it 0.3 compatible, and here it is.
An example can be found at https://www.serverffs.com/dump/query.php

This works on PHP 5 only, if you have PHP 4 you should just update to PHP 5 which got released 6 years ago.
If you have shared webhosting, tell your provider to update or switch to a modern provider.


Usage:
Code:
<?php
require "samp_query.php";

$serverIP = "91.121.209.20";
$serverPort = 7777;

try
{
    $rQuery = new QueryServer( $serverIP, $serverPort );

    $aInformation = $rQuery->GetInfo( );
    $aServerRules = $rQuery->GetRules( );
    $aBasicPlayer = $rQuery->GetPlayers( );
    $aTotalPlayers = $rQuery->GetDetailedPlayers( );

    $rQuery->Close( );
}
catch (QueryServerException $pError)
{
    echo 'Server is offline';
}

if(isset($aInformation) && is_array($aInformation)){
?>
  <b>General Information</b>
  <table width="400">
      <tr>
          <td>Hostname</td>
          <td><?php echo htmlentities($aInformation['Hostname']); ?></td>
      </tr>
      <tr>
          <td>Gamemode</td>
          <td><?php echo htmlentities($aInformation['Gamemode']); ?></td>
      </tr>
      <tr>
          <td>Players</td>
          <td><?php echo $aInformation['Players']; ?> / <?php echo $aInformation['MaxPlayers']; ?></td>
      </tr>
      <tr>
          <td>Map</td>
          <td><?php echo htmlentities($aInformation['Map']); ?></td>
      </tr>
      <tr>
          <td>Weather</td>
          <td><?php echo $aServerRules['weather']; ?></td>
      </tr>
      <tr>
          <td>Time</td>
          <td><?php echo $aServerRules['worldtime']; ?></td>
      </tr>
      <tr>
          <td>Version</td>
          <td><?php echo $aServerRules['version']; ?></td>
      </tr>
      <tr>
          <td>Password</td>
          <td><?php echo $aInformation['Password'] ? 'Yes' : 'No'; ?></td>
      </tr>
  </table>

  <br />
  <b>Online Players</b>
  <?php
  if(!is_array($aTotalPlayers) || count($aTotalPlayers) == 0){
      echo '<br /><i>None</i>';
  } else {
  ?>
	  <table width="400">
	      <tr>
	          <td><b>Player ID</b></td>
	          <td><b>Nickname</b></td>
	          <td><b>Score</b></td>
	          <td><b>Ping</b></td>
	      </tr>
	  <?php
	  foreach($aTotalPlayers AS $id => $value){
	  ?>
	      <tr>
	          <td><?php echo $value['PlayerID']; ?></td>
	          <td><?php echo htmlentities($value['Nickname']); ?></td>
	          <td><?php echo $value['Score']; ?></td>
	          <td><?php echo $value['Ping']; ?></td>
	      </tr>
	  <?php
	  }
	
	  echo '</table>';
	}
}
?>
Actual class (save as samp_query.php):
Code:
<?php
/*********************************************
*
* SA-MP Query Server Version 0.3
*
* This class provides you with an easy to use interface to query
* your SA-MP 0.2 servers. Usage is simple, but has changed a bit
* since the last version, so be sure to check out the examples
* that come along with this script. It is updated with some of
* the new SA-MP 0.2 query-techniques that can be used.
*
* Author: Peter Beverloo
*     peter@dmx-network.com
*     Ex SA-MP Developer
*
* Updated: Wouter van Eekelen
*     wouter.van.eekelen@serverffs.com
*     SA-MP Betatester
*********************************************/

class QueryServer
{
  // Private variables used for the query-ing.
  private $szServerIP;
  private $iPort;
  private $rSocketID;

  private $bStatus;

  // The __construct function gets called automatically
  // by PHP once the class gets initialized.
  function __construct( $szServerIP, $iPort )
  {
      $this->szServerIP = $this->VerifyAddress( $szServerIP );
      $this->iPort = $iPort;

      if (empty( $this->szServerIP ) || !is_numeric( $iPort )) {
          throw new QueryServerException( 'Either the ip-address or the port isn\'t filled in correctly.' );
      }

      $this->rSocketID = @fsockopen( 'udp://' . $this->szServerIP, $iPort, $iErrorNo, $szErrorStr, 5 );
      if (!$this->rSocketID) {
          throw new QueryServerException( 'Cannot connect to the server: ' . $szErrorStr );
      }

      socket_set_timeout( $this->rSocketID, 0, 500000 );
      $this->bStatus = true;
  }

  // The VerifyAddress function verifies the given hostname/
  // IP address and returns the actual IP Address.
  function VerifyAddress( $szServerIP )
  {
      if (ip2long( $szServerIP ) !== false && 
        long2ip( ip2long( $szServerIP ) ) == $szServerIP ) {
          return $szServerIP;
      }

      $szAddress = gethostbyname( $szServerIP );
      if ($szAddress == $szServerIP) {
          return "";
      }

      return $szAddress;
  }

  // The SendPacket function sends a packet to the server which
  // requests information, based on the type of packet send.
  function SendPacket( $cPacket )
  {
      $szPacket = 'SAMP';
      $aIpChunks = explode( '.', $this->szServerIP );

      foreach( $aIpChunks as $szChunk ) {
          $szPacket .= chr( $szChunk );
      }

      $szPacket .= chr( $this->iPort & 0xFF );
      $szPacket .= chr( $this->iPort >> 8 & 0xFF );
      $szPacket .= $cPacket;

      return fwrite( $this->rSocketID, $szPacket, strlen( $szPacket ) );
  }

  // The GetPacket() function returns a specific number of bytes
  // read from the socket. This uses a special way of getting stuff.
  function GetPacket( $iBytes )
  {
      $iResponse = fread( $this->rSocketID, $iBytes );
      if ($iResponse === false) {
          throw new QueryServerException( 'Connection to ' . $this->szServerIP . ' failed or has dropped.' );
      }

      $iLength = ord( $iResponse );
      if ($iLength > 0)
          return fread( $this->rSocketID, $iLength );

      return "";
  }

  // After we're done, the connection needs to be closed using
  // the Close() function. Otherwise stuff might go wrong.
  function Close( )
  {
      if ($this->rSocketID !== false) {
          fclose( $this->rSocketID );
      }
  }

  // A little function that's needed to properly convert the
  // four bytes we're recieving to integers to an actual PHP
  // integer. ord() can't handle value's higher then 255.
  function toInteger( $szData )
  {
      $iInteger = 0;

      $iInteger += ( ord( @$szData[ 0 ] ) );
      $iInteger += ( ord( @$szData[ 1 ] ) << 8 );
      $iInteger += ( ord( @$szData[ 2 ] ) << 16 );
      $iInteger += ( ord( @$szData[ 3 ] ) << 24 );

      if( $iInteger >= 4294967294 )
          $iInteger -= 4294967296;

      return $iInteger;
  }

  // The GetInfo() function returns basic information about the
  // server, like the hostname, number of players online etc.
  function GetInfo( )
  {
      if ($this->SendPacket('i') === false) {
          throw new QueryServerException( 'Connection to ' . $this->szServerIP . ' failed or has dropped.' );
      }

      $szFirstData = fread( $this->rSocketID, 4 );
      if (empty( $szFirstData ) || $szFirstData != 'SAMP') {
          throw new QueryServerException( 'The server at ' . $this->szServerIP . ' is not an SA-MP Server.' );
      }

      // Pop the first seven characters returned.
      fread( $this->rSocketID, 7 );

      return array (
          'Password'  =>  ord( fread( $this->rSocketID, 1 ) ),
          'Players'  =>  $this->toInteger( fread( $this->rSocketID, 2 ) ),
          'MaxPlayers' =>  $this->toInteger( fread( $this->rSocketID, 2 ) ),
          'Hostname'  =>  $this->GetPacket( 4 ),
          'Gamemode'  =>  $this->GetPacket( 4 ),
          'Map'    =>  $this->GetPacket( 4 )
      );
  }

  // The GetRules() function returns the rules which are set
  // on the server, e.g. the gravity, version etcetera.
  function GetRules( )
  {
      if ($this->SendPacket('r') === false) {
          throw new QueryServerException( 'Connection to ' . $this->szServerIP . ' failed or has dropped.' );
      }

      // Pop the first 11 bytes from the response;
      fread( $this->rSocketID, 11 );

      $iRuleCount = ord( fread( $this->rSocketID, 2 ) );
      $aReturnArray = array( );

      for( $i = 0; $i < $iRuleCount; $i ++ ) {
          $szRuleName = $this->GetPacket( 1 );
          $aReturnArray[ $szRuleName ] = $this->GetPacket( 1 );
      }

      return $aReturnArray;
  }

  // The GetPlayers() function is pretty much simelar to the
  // detailed function, but faster and contains less information.
  function GetPlayers( )
  {
      if ($this->SendPacket('c') === false) {
          throw new QueryServerException( 'Connection to ' . $this->szServerIP . ' failed or has dropped.' );
      }

      // Again, pop the first eleven bytes send;
      fread( $this->rSocketID, 11 );

      $iPlayerCount = ord( fread( $this->rSocketID, 2 ) );
      $aReturnArray = array( );

      for( $i = 0; $i < $iPlayerCount; $i ++ )
      {
          $aReturnArray[ ] = array (
              'Nickname' => $this->GetPacket( 1 ),
              'Score'  => $this->toInteger( fread( $this->rSocketID, 4 ) )
          );
      }

      return $aReturnArray;
  }

  // The GetDetailedPlayers() function returns the player list,
  // but in a detailed form inclusing the score and the ping.
  function GetDetailedPlayers( )
  {
      if ($this->SendPacket('d') === false) {
          throw new QueryServerException( 'Connection to ' . $this->szServerIP . ' failed or has dropped.' );
      }

      // Skip the first 11 bytes of the response;
      fread( $this->rSocketID, 11 );

      $iPlayerCount = ord( fread( $this->rSocketID, 2 ) );
      $aReturnArray = array( );

      for( $i = 0; $i < $iPlayerCount; $i ++ ) {
          $aReturnArray[ ] = array(
              'PlayerID'  => $this->toInteger( fread( $this->rSocketID, 1 ) ),
              'Nickname'  => $this->GetPacket( 1 ),
              'Score'   => $this->toInteger( fread( $this->rSocketID, 4 ) ),
              'Ping'    => $this->toInteger( fread( $this->rSocketID, 4 ) )
          );
      }

      return $aReturnArray;
  }

function RCON($rcon, $command)
  {
      echo 'Password '.$rcon.' with '.$command;
      if ($this->SendPacket('x '.$rcon.' '.$command) === false) {
          throw new QueryServerException( 'Connection to ' . $this->szServerIP . ' failed or has dropped.' );
      }

      // Pop the first 11 bytes from the response;
      $aReturnArray = fread( $this->rSocketID, 11 );

      echo fread( $this->rSocketID, 11 );

      return $aReturnArray;
  }

}

/*********************************************
*
* The QueryServerException is used to throw errors when querying
* a specific server. That way we force the user to use proper
* error-handling, and preferably even a try-/catch statement.
*
**********************************************/

class QueryServerException extends Exception
{
  // The actual error message is stored in this variable.
  private $szMessage;

  // Again, the __construct function gets called as soon
  // as the exception is being thrown, in here we copy the message.
  function __construct( $szMessage )
  {
      $this->szMessage = $szMessage;
  }

  // In order to read the exception being thrown, we have
  // a .NET-like toString() function, which returns the message.
  function toString( )
  {
      return $this->szMessage;
  }
}
?>
Woet is offline   Reply With Quote
Old 29/09/2009, 10:19 AM   #2
[LL]InstabiC
Big Clucker
 
Join Date: Aug 2009
Posts: 90
Reputation: 0
Default Re: PHP Query (show your server on your website)

Thanks woet.
[LL]InstabiC is offline   Reply With Quote
Old 29/09/2009, 03:05 PM   #3
Fj0rtizFredde
Gangsta
 
Fj0rtizFredde's Avatar
 
Join Date: Jun 2008
Location: Sweden
Posts: 888
Reputation: 120
Default Re: PHP Query (show your server on your website)

Nice Just what I needed
__________________
Latest crap I've made:
[Include]CleverBot - Never Be Alone Again!
Fj0rtizFredde is offline   Reply With Quote
Old 29/09/2009, 07:48 PM   #4
[mad]MLK
Huge Clucker
 
Join Date: Mar 2009
Posts: 349
Reputation: 0
Default Re: PHP Query (show your server on your website)

Nice one woet, I'm integrating this into my control panel I'm coding, Thanks for the help!.
[mad]MLK is offline   Reply With Quote
Old 30/09/2009, 06:08 PM   #5
eXchainZ-FoReVeR
Huge Clucker
 
Join Date: Jan 2009
Posts: 294
Reputation: 0
Default Re: PHP Query (show your server on your website)

Instead of <?php ?> lots of time, Add one at Top of the Script and End of the script, could be better :P
eXchainZ-FoReVeR is offline   Reply With Quote
Old 30/09/2009, 06:14 PM   #6
shady91
High-roller
 
shady91's Avatar
 
Join Date: Nov 2008
Location: Uk
Posts: 1,593
Reputation: 7
Default Re: PHP Query (show your server on your website)

Quote:
Originally Posted by ExChainZ | 24Hosting4You.NET
Instead of <?php ?> lots of time, Add one at Top of the Script and End of the script, could be better :P
i think woet noes how to code just fine
shady91 is offline   Reply With Quote
Old 30/09/2009, 06:17 PM   #7
Calgon
Banned
 
Join Date: Jan 2009
Posts: 6,145
Reputation: 1127
Default Re: PHP Query (show your server on your website)

Quote:
Originally Posted by ExChainZ | 24Hosting4You.NET
Instead of <?php ?> lots of time, Add one at Top of the Script and End of the script, could be better :P
Don't comment uselessly. Doing so in that method would waste time, him terminating the document parsing as PHP and reverting to HTML is what saves him having to add backslashes after every quote (single or double) in an echo(). Also, having to process the backslashes is more intensive.
Calgon is offline   Reply With Quote
Old 30/09/2009, 06:19 PM   #8
eXchainZ-FoReVeR
Huge Clucker
 
Join Date: Jan 2009
Posts: 294
Reputation: 0
Default Re: PHP Query (show your server on your website)

Quote:
Originally Posted by Shady91
Quote:
Originally Posted by ExChainZ | 24Hosting4You.NET
Instead of <?php ?> lots of time, Add one at Top of the Script and End of the script, could be better :P
i think woet noes how to code just fine
Woet is quite awesome at PHP Coding, but I was just giving him a small suggestion.

Quote:
Originally Posted by CalgonX
Quote:
Originally Posted by ExChainZ | 24Hosting4You.NET
Instead of <?php ?> lots of time, Add one at Top of the Script and End of the script, could be better :P
Don't comment uselessly. Doing so in that method would waste time, him terminating the document parsing as PHP and reverting to HTML is what saves him having to add backslashes after every quote (single or double) in an echo().
And you chill out; its only a simple reply.
eXchainZ-FoReVeR is offline   Reply With Quote
Old 30/09/2009, 06:20 PM   #9
Calgon
Banned
 
Join Date: Jan 2009
Posts: 6,145
Reputation: 1127
Default Re: PHP Query (show your server on your website)

Quote:
Originally Posted by ExChainZ | 24Hosting4You.NET
Quote:
Originally Posted by Shady91
Quote:
Originally Posted by ExChainZ | 24Hosting4You.NET
Instead of <?php ?> lots of time, Add one at Top of the Script and End of the script, could be better :P
i think woet noes how to code just fine
Woet is quite awesome at PHP Coding, but I was just giving him a small suggestion.

Quote:
Originally Posted by CalgonX
Quote:
Originally Posted by ExChainZ | 24Hosting4You.NET
Instead of <?php ?> lots of time, Add one at Top of the Script and End of the script, could be better :P
Don't comment uselessly. Doing so in that method would waste time, him terminating the document parsing as PHP and reverting to HTML is what saves him having to add backslashes after every quote (single or double) in an echo().
And you chill out; its only a simple reply.
It's a useless suggestion - When you try suggesting something; maybe you should actually provide a useful reply.
Calgon is offline   Reply With Quote
Old 30/09/2009, 06:21 PM   #10
shady91
High-roller
 
shady91's Avatar
 
Join Date: Nov 2008
Location: Uk
Posts: 1,593
Reputation: 7
Default Re: PHP Query (show your server on your website)

ok lets not spam this topic with a argument please.
shady91 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[MySQL] samp-server quits when you run query dioguitosbr Help Archive 2 09/11/2009 07:03 PM
Server crash after sending query to mysql Vince Help Archive 0 15/07/2009 11:45 AM
Server query url? kazimieras Server Support 2 02/03/2009 11:11 PM
Server Query Joe_Dean Server Support 6 26/01/2009 11:33 PM


All times are GMT. The time now is 02:50 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.