?
Solved

CS Live Server Stats with PHP

Posted on 2004-03-31
7
Medium Priority
?
4,041 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:Andy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 1

Accepted Solution

by:
Snyke earned 2000 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:Andy
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:Andy
ID: 10734335
Also, it doesn't always pull out all the players that are on the server.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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:Andy
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
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…
Suggested Courses

765 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