Author Topic: PHP Server Queries Useful?  (Read 745 times)

0 Members and 1 Guest are viewing this topic.

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • ****
  • Posts: 877
  • Karma: 54
    • View Profile
    • Dev-Solutions
PHP Server Queries Useful?
« on: June 07, 2010, 08:07:30 PM »
Well, after doing some research. I've come up with this little piece of PHP code:

Code: (php) [Select]
<?php
function source_query($ip){
    
$cut explode(":"$ip);
    
$HL2_address $cut[0];
    
$HL2_port $cut[1];
$HL2_stats '';
$results = array();

    
$HL2_command "\377\377\377\377TSource Engine Query\0";
    
    
$HL2_socket fsockopen("udp://".$HL2_address$HL2_port$errno$errstr,3);
    
fwrite($HL2_socket$HL2_command); 
    
$JunkHead fread($HL2_socket,4);
    
$CheckStatus socket_get_status($HL2_socket);
if($CheckStatus["unread_bytes"] == 0)return 0;

$i 1;
    while(
$i){
        
$str fread($HL2_socket,1);
        
$HL2_stats .= $str;
        
$CheckStatus socket_get_status($HL2_socket);
        if(
$CheckStatus["unread_bytes"] == 0){
               
$i 0;
        }
    }

fclose($HL2_socket);

$HL2_stats substr($HL2_stats,1);
$HL2_stats explode('|||',urldecode(str_replace(array('%07''%00'),array('','|||'),urlencode($HL2_stats))));
foreach($HL2_stats as $k => $v){
switch($k){
case '0':
$results['hostname'] = $v;
case '1':
$results['map'] = $v;
case '2':
$results['game'] = $v;
case '3':
$results['gamemode'] = $v;
}
}
return $results;
}
?>


<?php
$ip 
'74.193.56.8:27015';
$results source_query($ip);
foreach(
$results as $k => $v){
echo $k' = ' .$v'<br />';
}
?>


Which in the browser, returns this result:
Code: [Select]
hostname = Jay209015 Dev Server
map = gm_flatgrass
game = garrysmod
gamemode = sandbox

I don't know if this is useful or not, but I have a feeling a lot more can be done with this. I just don't fully understand these server queries.
http://developer.valvesoftware.com/wiki/Source_Server_Queries

An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • ****
  • Posts: 3352
  • Karma: 108
  • Project Specialist
    • View Profile
    • Team Ulysses [ULib/ULX, other fine releases]
Re: PHP Server Queries Useful?
« Reply #1 on: June 07, 2010, 08:18:53 PM »
Now that there are/will be linux binaries, a self-monitoring script would be good to tell if the server had locked up rather than actually crashing. If locked up, kill the process, then restart the server.
(actually I guess this could be done for Windows too, but linux by default has more scripting languages included)
Megiddo had code at one time that would do that when we hosted a gmod server. Problem is, I think the server queries changed and the script broke.
Or something like that.

Your PHP code bit is a good sample bit.
It's probably basically the same that many of the server signature status sites use, then mix with the PHP image module to make them.
Software Upgrade Paradox - If you improve a piece of software enough times, you eventually ruin it - David Pogue

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • ****
  • Posts: 877
  • Karma: 54
    • View Profile
    • Dev-Solutions
Re: PHP Server Queries Useful?
« Reply #2 on: June 07, 2010, 08:41:54 PM »
Well, made a few more updates. Version number, dedicated(bool), and OS:

Code: (php) [Select]
<?php
function source_query($ip){
    
$cut explode(":"$ip);
    
$HL2_address $cut[0];
    
$HL2_port $cut[1];
$HL2_stats '';
$results = array();

    
$HL2_command "\xFF\xFF\xFF\xFFTSource Engine Query\x00";
    
    
$HL2_socket fsockopen("udp://".$HL2_address$HL2_port$errno$errstr,3);
    
fwrite($HL2_socket$HL2_command); 
    
$JunkHead fread($HL2_socket,4);
    
$CheckStatus socket_get_status($HL2_socket);
if($CheckStatus["unread_bytes"] == 0)return 0;

$i 1;
    while(
$i){
        
$str fread($HL2_socket,1);
        
$HL2_stats .= $str;
        
$CheckStatus socket_get_status($HL2_socket);
        if(
$CheckStatus["unread_bytes"] == 0){
               
$i 0;
        }
    }

fclose($HL2_socket);

$HL2_stats substr($HL2_stats,1);
$HL2_stats explode('|||',urldecode(str_replace(array('%07''%00'),array('','|||'),urlencode($HL2_stats))));
foreach($HL2_stats as $k => $v){
switch($k){
case '0':
$results['Hostname'] = $v;
case '1':
$results['Map'] = $v;
case '2':
$results['Game'] = $v;
case '3':
$results['Gamemode'] = $v;
case '5':
if(substr($v,0,1) == 'd'){
$results['Dedicated'] = 'True';
}else{
$results['Dedicated'] = 'False';
}
if(substr($v,1,1) == 'w'){
$results['OS'] = 'Windows';
}else{
$results['OS'] = 'Linux';
}
case '7':
$results['Version'] = $v;
}
}
return $results;
}
?>


<?php
$ip 
'74.193.56.8:27015';
$results source_query($ip);
foreach(
$results as $k => $v){
echo $k' = ' .$v'<br />';
}
?>

Output:
Code: [Select]
Hostname = Jay209015 Dev Server
Map = gm_flatgrass
Game = garrysmod
Gamemode = sandbox
Dedicated = False
OS = Linux
Version = 1.0.0.76

An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • ****
  • Posts: 3586
  • Karma: 191
  • Project Lead
    • View Profile
Re: PHP Server Queries Useful?
« Reply #3 on: June 08, 2010, 04:46:02 AM »
I already made this and released it on the forums, search for SrcQR.
This space for rent

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • ****
  • Posts: 877
  • Karma: 54
    • View Profile
    • Dev-Solutions
Re: PHP Server Queries Useful?
« Reply #4 on: June 08, 2010, 07:20:59 AM »
I already made this and released it on the forums, search for SrcQR.
     Lol, and I thought I was making something new :P

An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • ****
  • Posts: 877
  • Karma: 54
    • View Profile
    • Dev-Solutions
Re: PHP Server Queries Useful?
« Reply #5 on: June 08, 2010, 08:33:20 AM »
Either way, I think I'm about to start working on a CMS for ULX. You're classes will make this a lot easier, so we'll see how this turns out.

An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • ****
  • Posts: 3586
  • Karma: 191
  • Project Lead
    • View Profile
Re: PHP Server Queries Useful?
« Reply #6 on: June 08, 2010, 09:46:00 AM »
Sounds fun :)
This space for rent

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • ****
  • Posts: 877
  • Karma: 54
    • View Profile
    • Dev-Solutions
Re: PHP Server Queries Useful?
« Reply #7 on: June 08, 2010, 11:35:37 AM »
My question is, what all should this CMS do?

Currently thinking of a scoreboard style display with a drop down on each for: kick, ban, adduser, pm, gimp....ect...
Group, Adverts, Bans, and MOTD management.
Couple of small features like average ping/players and server activity based on times of the day.
Auto map changer for times of the day.
Chat logging.
Perhaps a ulx autoresponse for help...?

Open to more suggestions.

An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • ****
  • Posts: 877
  • Karma: 54
    • View Profile
    • Dev-Solutions
Re: PHP Server Queries Useful?
« Reply #8 on: June 11, 2010, 10:32:22 AM »
Template, what do y'all think?


An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • ****
  • Posts: 3586
  • Karma: 191
  • Project Lead
    • View Profile
Re: PHP Server Queries Useful?
« Reply #9 on: June 11, 2010, 11:50:48 AM »
Looks nice. Are you making all tabs load with page (javascript)? Or does it fetch a new page when you click on a tab (PHP)? I think it'd be snazzy if you made them all load at once so you could switch between them easily, but I guess that largely depends on what you're going to put in those tabs and how big the contents are.
This space for rent

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • ****
  • Posts: 877
  • Karma: 54
    • View Profile
    • Dev-Solutions
Re: PHP Server Queries Useful?
« Reply #10 on: June 11, 2010, 12:41:44 PM »
I'm going to load them all with javascript.

Edit: at least that's the plan :P
« Last Edit: June 11, 2010, 01:54:30 PM by jay209015 »

An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly

Offline Rambomst

  • Newbie
  • *
  • Posts: 18
  • Karma: 0
    • View Profile
Re: PHP Server Queries Useful?
« Reply #11 on: June 11, 2010, 06:53:56 PM »
It looks good. I am currently in the process of making my own query script so far it looks like this I am going to be adding some ajax to make it refresh the stats every like 5 seconds and I need to style the player lists a little bit/fix some coding bugs here and there.

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • ****
  • Posts: 877
  • Karma: 54
    • View Profile
    • Dev-Solutions
Re: PHP Server Queries Useful?
« Reply #12 on: June 11, 2010, 08:08:31 PM »
How are you queries running so fast?

An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly

Offline Rambomst

  • Newbie
  • *
  • Posts: 18
  • Karma: 0
    • View Profile
Re: PHP Server Queries Useful?
« Reply #13 on: June 11, 2010, 08:40:48 PM »
My webserver and gaming servers are hosted in the same rack and I query them using a local IP maybe that is why.

Offline jay209015

  • Ulysses Team Member
  • Hero Member
  • ****
  • Posts: 877
  • Karma: 54
    • View Profile
    • Dev-Solutions
Re: PHP Server Queries Useful?
« Reply #14 on: June 12, 2010, 08:09:09 AM »
Would you mind sharing some code?

An error only becomes a mistake when you refuse to correct it. --JFK

"And thus the downfall of the great ULX dynasty was wrought not by another dynasty, but the slow and steady deterioration of the leaders themselves, followed by the deprecation of the great knowledge they possessed." -Gmod, Chapter 28, verse 34 -- Stickly