How to add status containing players online coded in PHP ?
<?php
require_once ('config.php');
echo '<font style="color: #808080" size="2">SERVER:</font> ';
$fp = @fsockopen($serveraddress, $serverport, $errno, $errstr, 1);
if (!$fp) {
echo '<font style="color: red" size="2"><b>MAINT.</b></font><br />';
}
else
{
echo '<font style="color: YellowGreen" size="2"><b>ONLINE</b></font><br />';
fclose($fp);
}
$rank = mysql_query("SELECT Online FROM Online WHERE Online > 0 || Online = 0");
$row = mysql_fetch_array($rank);
$tot = $row['Online'];
echo "<font color='#808080' size='2'><b>PLAYER:</font><font color='YellowGreen' size='2'>$tot</font>";
?>
Or:Quote:
PHP Code:<?php
require_once ('config.php');
echo '<font style="color: #808080" size="2">SERVER:</font> ';
$fp = @fsockopen($serveraddress, $serverport, $errno, $errstr, 1);
if (!$fp) {
echo '<font style="color: red" size="2"><b>MAINT.</b></font><br />';
}
else
{
echo '<font style="color: YellowGreen" size="2"><b>ONLINE</b></font><br />';
fclose($fp);
}
$rank = mysql_query("SELECT Online FROM Online WHERE Online > 0 || Online = 0");
$row = mysql_fetch_array($rank);
$tot = $row['Online'];
echo "<font color='#808080' size='2'><b>PLAYER:</font><font color='YellowGreen' size='2'>$tot</font>";
?>
one little thing in this code, other then that the process seems about right.Quote:
PHP Code:echo "<font color='#808080' size='2'><b>PLAYER:</font><font color='YellowGreen' size='2'>$tot</font>";
echo "<font color='#808080' size='2'><b>PLAYER:</font><font color='YellowGreen' size='2'>" . $tot . "</font>";
$rank = mysql_query("SELECT Online FROM Online WHERE Online > 0 || Online = 0");
" if the website is down" sorry, do you mean the database?Quote:
That's actually one of the worst ways of doing it as if the website is down you have to wait, and wait, and wait for the script to time out before the page loads.
i agree. but again, a simple mysql connection check would do the trick:Quote:
And even when editing max_execution_time you dont solve this problem as then it cuts the entire script short
$conn= mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$conn) {
die('sql is down: ' . mysql_error()); // edit if you don't want to post the mysql error out.
}
echo 'connection established'; // edit out or whatnot.
// manipulate your data here.
mysql_close($conn);
well you could lower the mysql connection time or optimize the MySQL response time (disable MySQL indexes. disable DNS hostname lookup, activate query cache), but you still have to get a response from the database (and time for that is requires) in order to proceed with data in your page.Quote:
And again you dont solve the problem, you still rely on the fact that there is a timeout so this will never be instant.
$sv_status = True; // server status variable (for example only)
if ($sv_status == TRUE) {
// data goes here.
}
wouldn't that defeat the purpose of going around the time it takes you to connect to the database and check a table from the webpage?Quote:
The best solution is to have the game server update a database stored on the webserver, with a timestamp of when it last wrote, your page then checks the timestamp and if the timestamp is more than say...2 - 5 minutes out of date then you know the server is down. All online player counts and similar statistics are also stored on the webservers database.
$address=$_SERVER['REMOTE_ADDR'];
if (isset($_REQUEST['port']) and
(!strlen($_REQUEST['port'])==0))
$port=$_REQUEST['port'];
else
unset($port);
if (isset($port) and
($socket=socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) and
(socket_connect($socket, $address, $port)))
{
$text="Connection ok on IP $address, port $port";
socket_close($socket);
}
else
$text="Unable to connect<pre>".socket_strerror(socket_last_error())."</pre>";
echo "<html><head></head><body>".
$text.
"</body></html>";