Solved

CS Live Server Stats with PHP

Posted on 2004-03-31
7
3,993 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

840 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