Solved

CS Live Server Stats with PHP

Posted on 2004-03-31
7
3,987 Views
Last Modified: 2007-12-19
I've been asked to write a small script to be included on a clan website that retrieves the number of players, the total number of player slots, the current map, all the players, their scores etc from the clans Counter-Strike server when a visitor clicks a link. I can use PHP and I have admin rights on my own clan server so I'm familiar with the HL console but I have no clue how to go about getting that information using PHP.

Don't know if anyone here has experience with this but any help would be appreciated.

Cheers!
0
Comment
Question by:Reapz
  • 3
  • 3
7 Comments
 
LVL 1

Accepted Solution

by:
Snyke earned 500 total points
ID: 10734136
There are many premade scripts out there that allow you to query your server. If you are just looking for a quick solution then I suggest you to take a look at http://demo.kquery.com/phpquery100.zip which is a rather complete script with some nice functions.

If you want to make your own script instead then you will have to understand how the HL-Protocol works ( http://dev.kquery.com/index.php?article=31 : this is a page which tries to explain it) and last but not least you will have to know a bit about UDP sockets for the CS-Server - Web-Server communication.

IMPORTANT: You will have to make sure your web-server allows fsockopen or you won't even be able to contact the CS-Server.
0
 
LVL 6

Author Comment

by:Reapz
ID: 10734307
Ok... I've managed to put together the following which gets the infostring section. It puts everything ito a nice array for easy reference. For exampe, I can output the map name by doing echo $ServerData['map'];. What this doesn't do is get player info.

I tried changing $ServerinfoCommand = "\377\377\377\377infostring\0";  to $ServerinfoCommand = "\377\377\377\377players\0"; and output all the array values to see if would be that easy. Course it wasn't! The only recognisable stuff in there are the player names. How could I adapt this code to pull out the player info? Also, I notice that on the second link you posted above it makes no refernce to player deaths only frags so how do programs like HLSW get the player deaths?


<?php
// IP address
$ServerIP = "195.20.108.64";
// Server port
$ServerPort = "27030";
// Create the variables of the info command to send to the server.
$ServerinfoCommand = "\377\377\377\377infostring\0";

// Open a connection to the server
$fp = fsockopen("udp://".$ServerIP, $ServerPort, $errno, $errstr);

//Send the request to the server.
fwrite($fp, $ServerinfoCommand);

//Remove the junk headers sent back
$JunkHead = fread($fp,24);

// Check to see if the server is running
$CheckStatus = socket_get_status($fp);
if($CheckStatus["unread_bytes"] == 0)
{
    die("Unable to connect to the server, ensure the IP and port is correct and that the server is running.");
}

// Read through the returned data and put in variable
$do = 1;
$HLServerStats= "";
while($do)
{
    $str = fread($fp,1);
    $HLServerStats.= $str;
    $status = socket_get_status($fp);
    if($status["unread_bytes"]  == 0) {$do = 0;}
}
//Close the connection
fclose($fp);

// Explode the packet into an array.
$HLServerStats = explode("\\", $HLServerStats);

// Count the amount of keys in the array.
$count = count($HLServerStats);

// The amount of keys in the array MUST be an even number
if($count % 2 == 0)
{
    // Loop though all the keys and put them in the $ServerData array with the key values.
    $i = 0;
    while($count != $i)
    {
        $ServerData[$HLServerStats[$i]] = $HLServerStats[$i+1];
        $i = $i + 2;
    }
}
?>
0
 
LVL 6

Author Comment

by:Reapz
ID: 10734335
Also, it doesn't always pull out all the players that are on the server.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Expert Comment

by:Snyke
ID: 10734432
Ok I found another resource for you to look at:
http://0x00.org/random/Halflife/QueryingGameServers.txt

The interesting part is this:
"players"
      Server responds with the following packet:
      (int32)            -1
      (byte)            ASCII 'D' (players response, S2A_PLAYER)
      (byte)            active client count

      for each active client
            (byte)            client number / index
            (string)            player name
            (int32)            client's frag total
            (float32)            client's total time in-game

This describes the data structure that builds up the server answer. Try sending a "players" to the server and look at the response, I'll try to make a script that parses the answers.
0
 
LVL 6

Author Comment

by:Reapz
ID: 10735706
Don't worry mate I worked it out thanks to the resources you provided.

Cheers for your help!
0
 
LVL 1

Expert Comment

by:Snyke
ID: 10736423
Thanks a lot dude :)
Hope your work's going on greatly.
0
 

Expert Comment

by:someonePFFT
ID: 21253121
Does anyone have a more updated script that works with Steam?
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
str_replace not working in php script 4 30
PHP Sessions in WordPress 8 31
hasing a url 16 28
google analytics on wp-admin password protected area 2 23
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question