Solved

CS Live Server Stats with PHP

Posted on 2004-03-31
7
3,980 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now