Solved

CS Live Server Stats with PHP

Posted on 2004-03-31
7
3,973 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
Comment Utility
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
Comment Utility
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
Comment Utility
Also, it doesn't always pull out all the players that are on the server.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 1

Expert Comment

by:Snyke
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks a lot dude :)
Hope your work's going on greatly.
0
 

Expert Comment

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

772 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

12 Experts available now in Live!

Get 1:1 Help Now