Development with PHPrcon
Why development with PHPrcon?
Imagine your clan homepage. Full of information about your history, your clanmembers and maybe some stats
of your clanserver. Imagine the info page of your clanserver. IP adress, port, the server rules and somewhere
the name and a pre screenshot of the current map. Imagine a list of the current players. Their frags, online time,
even a list of the WonID's and the IP's of all players on the gameserver.
I will try to teach you how to do that easily with PHPrcon.
What is rcon_net.php?
rcon_net.php is the heart of PHPrcon. rcon_net.php defines the PHP class rcon. This class does all
communication between PHPrcon and the gameserver. Because of the restructuring and refactoring since PHPrcon
V0.6, the internal structure got that easy that only few rows of PHP code are enough for a command sent
to the gameserver.
include("rcon_net.php");
$server = new rcon();
if($server->connect($server_ip, $server_port, $server_password))
{
$list = $server->rcon_command("listplayers");
$server->disconnect();
}
The example connects to a gameserver with ip stored in $server_ip, port number stored in $server_port and
the rcon password stored in $server_password and saves the list of current players in $list.
What is rcon and public protocoll?
There are two ways to connect to a gameserver from outside. The first is the public protocoll. Halflife or
any HalflifeMod use this protocoll to get information about current players and server rules at Server Info
at the server list. No rcon password is needed to excess a gameserver through the public protocoll, because the
only way to use it is to get non-sensible information. No IP's or WonID's of the players are sent throughout
the net.
The second way is the rcon protocoll. HSLW or other remote controll tools use this protocoll to administrate
gameserver from outside the game. So does PHPrcon. It is neccessary to know the correct rcon password, for
have non-restricted admin rights on this server. So be carefull who you give access to PHPrcon.
Both protocolls are part of PHPrcon and will be described here.
Using the rcon protocoll
You allready know how to use the rcon protocoll with PHPrcon, since you read the example above properly ;) .
Below you see the example again explained in steps.
include("rcon_net.php");
$server = new rcon();
if($server->connect($server_ip, $server_port, $server_password))
{
$result = $server->rcon_command("listplayers");
$server->disconnect();
}
| 1. | Include the file "rcon_net.php"
|
| 2. | Instanciate the class rcon
|
| 3. |
The method connect of class rcon builds the rcon connection to the gameserver. The parameters are
· Gameserver's IP
· Gameserver's Port number
· Gameserver's rcon password
The method connect returns
· true if successfully connected
· false if connection attempt failed
If you try to connect to often to a gamerserver with the wrong rcon password, the IP of the intruder will be
banned forever by the Halflife or HalflifeMod server.
|
| 4. | The method rcon_command sends a command to the gameserver. The parameters are
· rcon command that should be send to gameserver
· optional parameter pagenumber, starts with 0
The method rcon_command returns
· the commando result as formatted string
· "Bad rcon_password." if given rcon password in method connect is wrong
The command given as parameter is the same that would be given in the In-Game-Console.
If the result is bigger than approcimately 1.4 kB the basic Halflife machine sends the formatted
string in chunks. The problem is that no one knows, what is the last chunk sent. Since connection
timeouts are not part of all PHP versions, you may specify the pagenumber you want to see
by yourself.
| | 5. | The method disconnect closes the connection to the gameserver. |
Using the public protocoll
The public protocoll can be accessed through the three methods info, players and serverrules.
The results are returned in multidimensional arrays. The eample shows the use of the three methods:
include("rcon_net.php");
$server = new rcon();
if($server->connect($server_ip, $server_port))
{
$result1 = $server->info();
$result2 = $server->players();
$result3 = $server->serverrules();
$server->disconnect();
}
| 1. | Include the file "rcon_net.php"
|
| 2. | Instanciate the class rcon
|
| 3. |
The method connect of class rcon builds the rcon connection to the gameserver. The parameters are
· Gameserver's IP
· Gameserver's Port number
The method connect returns
· true if successfully connected
· false if connection attempt failed
|
| 4. | The method info fetches global gameserver information. It returns the array
| Array Key |
Description |
| "activeplayers" |
Active players on gameserver |
| "game" |
Exact Gamename |
| "ip" |
IP Adress of the gameserver |
| "map" |
Name of current map |
| "maxplayers" |
Max allowed players on gameserver |
| "mod" |
Exact Modname |
| "name" |
Name of gameserver |
|
If you want to display the current map name in the example above, use
echo($result1["map"]);
|
| 5. | The method infoplayers fetches some information about the active players on
the gameserver. It returns the array
| Array Key |
Description |
| # Player |
Number of the player starting with 1 |
| |
"name" |
Name of the player |
| |
"frag" |
Number of frags the player made |
| |
"time" |
Online time of the player. Format mm:ss |
|
Assume that there are 8 active players. If you want to display the name an the online time of the third player
in the example above, use
echo($result2[3]["name"] . " " . $result2[3]["time"]);
|
| 6. | The method serverrules fetches names and values of all set server
variables. It returns the array
| Array Key |
Description |
| Name of the server variable |
Value of the server variable |
|
If you want to display the value of the server variable "mp_forcechasecam" in the example above, use
echo($result3["mp_forcechasecam"]);
|
| 7. | The method disconnect closes the connection to the gameserver. |
Even more methods
The following methods are internal methods, but the may be accessed as well. Therefor i will only introduce them in short.
Method: serverinfo
Provides all information for the serverinfo.php page. It returns a multidimensional array.
| Array Key |
Description |
| "ip" |
Gameserver IP |
| "name" |
Gameserver name |
| "map" |
Name of current map |
| "mod" |
Number of HalflifeMod version |
| "game" |
Game name |
| "activeplayers" |
Number of active players |
| "maxplayers" |
Number of maximum players |
| # Player |
Number of the rule starting with 1 |
| |
"name" |
Name of the player |
| |
"id" |
Gameserver internal ID |
| |
"wonid" |
Hmmm, what would that be? |
| |
"frag" |
Number of frags of the player |
| |
"time" |
Online time of the player |
| |
"ping" |
Pingtime from Gameserver to the player |
| |
"adress" |
IP adress of the player |
|
Method: servermaps
Provides all information for the servermaps.php page. It returns a multidimensional array.
| Array Key |
Description |
| Name of directory |
Name of the directory as the mapping key |
| |
# Map |
Name of the map |
|
Some remarks
If you use PHPrcon as the base of your website, I would appreciate a short hint on your site that you used my
scripts. :)
|
|
|
|
Last Update: 20 Mar 2003
|