PHP Socket help, trying to make server monitor
12-22-2007, 05:07 PM
|
PHP Socket help, trying to make server monitor
|
Posts: 71
|
I'm trying to make a game server monitor. This script works perfectly well on my localhost (using AppServ as my WAMP), but does not work when I upload to my hosting. I have two hosting accounts with different companies and it doesn't work for either, and I get the same error.
The error is "Buffer Empty!", which means it's not reading/writing the information.
Any help would be awesome, thanks.
Here is my source:
Code:
<body bgcolor='#000000'>
<?php
$ip = '8.9.9.13'; // Server IP
$port = '28960'; // Server Port
$cellColor = '#101010'; // Cell Background Color
$borderColor = '#9d9d9d'; // Table Border Color
$fontColor = '#ffffff'; // Text Color
$width = '300px'; // Overall Table Width
print '<style type="text/css">
.gentext {
font-family: serif;
color: '.$fontColor.';
font-size: 12pt;
}
.infotable {
border: 1px;
border-style: solid;
border-width: thin;
border-color: '.$borderColor.';
border-collapse: collapse;
}
</style>';
$challenge = "\xFF\xFF\xFF\xFFgetstatus";
$fp = @fsockopen ('udp://'.$ip, $port, $errno, $errstr, 1);
if (!$fp) {
print 'Connection Refused!';
}
stream_set_timeout ($fp, 1, 0);
stream_set_blocking ($fp, true);
fwrite ($fp, $challenge);
$buffer = fread ($fp, 4096);
fclose ($fp);
if (!$buffer) {
print 'Buffer Empty!';
}
$rawdata = explode ("\n", $buffer);
array_pop ($rawdata);
$rawitem = explode ("\\", $rawdata[1]);
foreach ($rawitem as $key => $value) {
$value = strtolower ($value);
$value = preg_replace ("/\^./", "", $value);
$rawitem[$key + 1] = preg_replace ("/\^./", "", $rawitem[$key + 1]);
$setting[$value] = $rawitem[$key + 1];
}
$data['gamemod'] = $setting['gamename'];
$data['hostname'] = $setting['sv_hostname'];
$data['mapname'] = $setting['mapname'];
$data['players'] = $rawdata[2] ? count($rawdata) - 2 : 0;
$data['maxplayers'] = $setting['sv_maxclients'];
$data['password'] = $setting['g_needpass'];
$data['gametype'] = $setting['g_gametype'];
$data['version'] = $setting['shortversion'];
array_shift ($rawdata);
array_shift ($rawdata);
foreach ($rawdata as $key => $value) {
preg_match("/(.*) (.*) \"(.*)\"/", $value, $match);
$player[$key + 1]['score'] = $match[1];
$player[$key + 1]['ping'] = $match[2];
$player[$key + 1]['name'] = $match[3];
$player[$key + 1]['name'] = preg_replace ("/\^./", "", $player[$key + 1]['name']);
}
print '<table border="1" class="infotable" width="'.$width.'">
<tr>
<td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>Name:</b></td>
<td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.$data['hostname'].'</td>
</tr>
<tr>
<td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>IP:</b></td>
<td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.$ip.':'.$port.'</td>
</tr>
<tr>
<td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>Game:</b></td>
<td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.$data['gamemod'].'</td>
</tr>
<tr>
<td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>Map:</b></td>
<td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.$data['mapname'].'</td>
</tr>
<tr>
<td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>Gametype:</b></td>
<td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.ucfirst ($data['gametype']).'</td>
</tr>
<tr>
<td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>Version:</b></td>
<td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.$data['version'].'</td>
</tr>
<tr>
<td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>Players:</b></td>
<td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.$data['players'].'</td>
</tr>
<tr>
<td bgcolor="'.$cellColor.'" align="center" class="gentext" colspan="3"><b>PLAYERLIST</b></td>
</tr>';
$playerCount = count ($player);
if ($player[1]['name'] == '') {
print '<tr>
<td bgcolor="'.$cellColor.'" align="center" class="gentext" colspan="3">The server is empty.</td>
</tr>';
} else {
print '<tr>
<td bgcolor="'.$cellColor.'" align="center" class="gentext"><b>Name</b></td>
<td bgcolor="'.$cellColor.'" align="center" class="gentext"><b>Score</b></td>
<td bgcolor="'.$cellColor.'" align="center" class="gentext"><b>Ping</b></td>
</tr>';
for ($i = 1; $i <= $playerCount; $i++) {
print '<tr>
<td bgcolor="'.$cellColor.'" align="center" class="gentext">'.$player[$i]['name'].'</td>
<td bgcolor="'.$cellColor.'" align="center" class="gentext">'.$player[$i]['score'].'</td>
<td bgcolor="'.$cellColor.'" align="center" class="gentext">'.$player[$i]['ping'].'</td>
</tr>';
}
}
print '</table>';
?>
|
|
|
|
12-23-2007, 02:12 PM
|
Re: PHP Socket help, trying to make server monitor
|
Posts: 71
|
Anyone??
|
|
|
|
12-24-2007, 02:14 AM
|
Re: PHP Socket help, trying to make server monitor
|
Posts: 1,226
Name: Mike
Location: Mataro, Spain
|
remove the @ from fsockopen first
|
|
|
|
12-24-2007, 02:16 AM
|
Re: PHP Socket help, trying to make server monitor
|
Posts: 1,226
Name: Mike
Location: Mataro, Spain
|
Also print out what is contained in your error variables, that is what they are for and it is called debugging.
|
|
|
|
12-24-2007, 05:09 PM
|
Re: PHP Socket help, trying to make server monitor
|
Posts: 71
|
I removed the @ and printed errno and errstr. errno returned "0", and errstr returned nothing.
It is connecting fine. The problem comes when I try to read/write the information.
|
|
|
|
12-24-2007, 07:25 PM
|
Re: PHP Socket help, trying to make server monitor
|
Posts: 13
Name: hach22
|
PHP Code:
stream_set_blocking ($fp,1);//second parameter is an integer stream_set_timeout ($fp,10);//1 is a small timeout!!!
PHP Code:
<?php
/** * @Author * @Copyright 2007 */
$ip = '8.9.9.13'; // Server IP $port = '28960'; // Server Port
$cellColor = '#101010'; // Cell Background Color $borderColor = '#9d9d9d'; // Table Border Color $fontColor = '#ffffff'; // Text Color
$width = '300px'; // Overall Table Width
print ' <html> <head> <title>COD4 SERVER MONITOR</title> </head> <style type="text/css"> .gentext { font-family: serif; color: '.$fontColor.'; font-size: 12pt; }
.infotable { border: 1px; border-style: solid; border-width: thin; border-color: '.$borderColor.'; border-collapse: collapse; } </style> <body bgcolor=#000000>';
$challenge = "\xFF\xFF\xFF\xFFgetstatus";
$fp = fsockopen ('udp://'.$ip, $port, $errno, $errstr, 1);
stream_set_timeout ($fp, 10, 0); stream_set_blocking ($fp, true); if (!$fp) { print 'Connection Refused!'; }
fwrite ($fp, $challenge);
$buffer = fread ($fp, 4096);
fclose ($fp);
if (!$buffer) { print 'Buffer Empty!'; }
$rawdata = explode ("\n", $buffer);
array_pop ($rawdata);
$rawitem = explode ("\\", $rawdata[1]);
foreach ($rawitem as $key => $value) { $value = strtolower ($value); $value = preg_replace ("/\^./", "", $value); $rawitem[$key + 1] = preg_replace ("/\^./", "", $rawitem[$key + 1]);
$setting[$value] = $rawitem[$key + 1]; }
$data['gamemod'] = $setting['gamename']; $data['hostname'] = $setting['sv_hostname']; $data['mapname'] = $setting['mapname']; $data['players'] = $rawdata[2] ? count($rawdata) - 2 : 0; $data['maxplayers'] = $setting['sv_maxclients']; $data['password'] = $setting['g_needpass']; $data['gametype'] = $setting['g_gametype']; $data['version'] = $setting['shortversion'];
array_shift ($rawdata); array_shift ($rawdata);
foreach ($rawdata as $key => $value) { preg_match("/(.*) (.*) \"(.*)\"/", $value, $match);
$player[$key + 1]['score'] = $match[1]; $player[$key + 1]['ping'] = $match[2]; $player[$key + 1]['name'] = $match[3]; $player[$key + 1]['name'] = preg_replace ("/\^./", "", $player[$key + 1]['name']);
}
print '<table border="1" class="infotable" width="'.$width.'"> <tr> <td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>Name:</b></td> <td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.$data['hostname'].'</td> </tr> <tr> <td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>IP:</b></td> <td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.$ip.':'.$port.'</td> </tr> <tr> <td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>Game:</b></td> <td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.$data['gamemod'].'</td> </tr> <tr> <td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>Map:</b></td> <td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.$data['mapname'].'</td> </tr> <tr> <td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>Gametype:</b></td> <td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.ucfirst ($data['gametype']).'</td> </tr> <tr> <td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>Version:</b></td> <td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.$data['version'].'</td> </tr> <tr> <td bgcolor="'.$cellColor.'" align="left" class="gentext"><b>Players:</b></td> <td bgcolor="'.$cellColor.'" align="left" class="gentext" colspan="2">'.$data['players'].'</td> </tr> <tr> <td bgcolor="'.$cellColor.'" align="center" class="gentext" colspan="3"><b>PLAYERLIST</b></td> </tr>'; $playerCount = count ($player); if ($player[1]['name'] == '') { print '<tr> <td bgcolor="'.$cellColor.'" align="center" class="gentext" colspan="3">The server is empty.</td> </tr>'; } else { print '<tr> <td bgcolor="'.$cellColor.'" align="center" class="gentext"><b>Name</b></td> <td bgcolor="'.$cellColor.'" align="center" class="gentext"><b>Score</b></td> <td bgcolor="'.$cellColor.'" align="center" class="gentext"><b>Ping</b></td> </tr>';
for ($i = 1; $i <= $playerCount; $i++) { print '<tr> <td bgcolor="'.$cellColor.'" align="center" class="gentext">'.$player[$i]['name'].'</td> <td bgcolor="'.$cellColor.'" align="center" class="gentext">'.$player[$i]['score'].'</td> <td bgcolor="'.$cellColor.'" align="center" class="gentext">'.$player[$i]['ping'].'</td> </tr>'; } } print '</table></body></html>';
Last edited by hach22; 12-24-2007 at 07:39 PM..
Reason: Commented and edited
|
|
|
|
12-25-2007, 09:21 PM
|
Re: PHP Socket help, trying to make server monitor
|
Posts: 71
|
Thanks, but that does the same thing. It works on my localhost but not on my webhost.
www.forsakenwarfare.net/monitor3.php
This is what I get.
|
|
|
|
|
« Reply to PHP Socket help, trying to make server monitor
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|