Server Status "Final Release" Done right
Ok, since that other poser doesn't even know how to fix it, I'll release a fixed source.
Save this as Server_Status.php
You will need to add the required information at the top.
Code:
<?
session_set_cookie_params(1200);
session_start();
// This function is to connect to the database
function connectDB() {
$con = mysql_connect("localhost", "Username", "Password") or die(mysql_error());
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Database") or die(mysql_error());
}
$Link = mysql_connect ("localhost", "Username", "Password");
$Result = mysql_db_query ("Database", "SELECT * from `serverstatus`", $Link);
$User = "Admin_Username";
$Pass = "Admin_Password";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Server Status Page</title>
</head>
<body>
<center>
<b>Server Status</b><br />
<img src="rslogo.png" alt="logo" /><br /><br />
<?php
if ($_GET['id'] == "register") {
echo "<form action=\"?id=submit\" method=\"post\">You can only add online servers to this list.<br /><br />Server Name:<br /><input type=\"text\" name=\"name\" /><br />Server IP:<br /><input type=\"text\" name=\"ip\" /><br />Port:<br /><input type=\"text\" name=\"port\" /><br /><br /><input type=\"submit\" value=\"Submit\" /></form><p><br />Click <a href=\"Server_Status.php\">here</a> to go to the main page.<br /></p>";
} else if($_GET['id'] == "submit") {
$Server_Name = $_POST['name']; $Server_IP = $_POST['ip']; $Server_Port = $_POST['port'];
if (($Server_Name != NULL) && ($Server_IP != NULL) && ($Server_Port != NULL)) {
$checkReg = @fsockopen("$Server_IP", "$Server_Port", $errno, $errstr, 1);
if($checkReg) {
connectDB();
mysql_query("SELECT * FROM `serverstatus` WHERE `Name`='$Server_Name' OR `IP`='$Server_IP'");
if(mysql_affected_rows() == 1){
mysql_close($con);
echo "That server is already on the list!";
} else{
mysql_query("INSERT INTO `serverstatus` (`Name`, `IP`, `Port`, `Uptime`, `Total`) VALUES ('$Server_Name', '$Server_IP', '$Server_Port', '1', '1')");
mysql_close($con);
echo "<br />Your Server has been successfully added to the list!";
}
} else {
echo "Invalid information or the server is down.<br />";
}
} else {
echo "You did not fill in all of the required information!";
}
} else if($_GET['id'] == "login") {
echo "<form action=\"?id=admin\" method=\"post\"><input type=\"text\" name=\"user\" /> ~Username<br /><br /><input type=\"password\" name=\"pass\" /> ~Password<br /><br /><input type=\"submit\" value=\"Submit\" /></form>";
} else if($_GET['id'] == "admin") {
if ($_SESSION['admin'] == 1) {
echo "<table><tr><td class=\"header\"><u>Server Name</u></td><td class=\"header\"><u>Server Status</u></td></tr>";
while ($Row = mysql_fetch_array ($Result)) {
$checkCurr = @fsockopen("$Row[IP]", "$Row[Port]", $errno, $errstr, 1);
echo "<tr><td class=\"lista\">$Row[Name]</td><td class=\"lista\">";
if($checkCurr) {
echo "<form action=\"Server_Status.php?id=delete_$Row[Name]\" method=\"post\"><input type=\"submit\" value=\" Online - Delete\" /></form></td></tr>";
} else {
echo "<form action=\"Server_Status.php?id=delete_$Row[Name]\" method=\"post\"><input type=\"submit\" value=\" Offline - Delete\" /></form></td></tr>";
}
}
echo "</table>";
mysql_close($con);
} else {
$Username = $_POST['user']; $Password = $_POST['pass'];
if ((isset($Username)) && (isset($Password)) && ($Username != NULL) && ($Password != NULL) && ($Username == $User) && ($Password == $Pass)) {
$_SESSION['admin'] = 1;
echo "Login successful!<br />Reload this page to continue.";
} else {
echo "Incorrect login information, please try again.";
}
}
} else {
$IDVAR = $_GET['id'];
if (isset($IDVAR)) {
if ($_SESSION['admin'] == 1) {
$remove = "delete_";
$ServName = str_replace($remove, "", $IDVAR);
connectDB();
mysql_query("DELETE FROM `serverstatus` WHERE `Name`='$ServName'") or die(mysql_error());
echo "Deleted successfully";
mysql_close($con);
}
} else {
echo "<table><tr><td class=\"header\"><u>Server Name</u></td><td class=\"header\"><u>Server Status</u></td><td class=\"header\"><u>IP Address</u></td><td class=\"header\"><u>Port</u></td><td class=\"header\"><u>Uptime</u></td></tr>";
if ($Result) {
while ($Row = mysql_fetch_array ($Result)) {
connectDB();
$checkCurr = @fsockopen("$Row[IP]", "$Row[Port]", $errno, $errstr, 1);
$total = $Row[Total] + 1;
echo "<tr><td class=\"lista\">$Row[Name]</td><td class=\"lista\">";
if($checkCurr) {
$uptime = $Row[Uptime] + 1;
echo "<img src=\"online.gif\" alt=\"online\" />";
$UpdateDB = mysql_query("UPDATE `serverstatus` SET `Uptime` = $uptime WHERE `Uptime` = $Row[Uptime]");
} else {
echo "<img src=\"offline.gif\" alt=\"offline\" />";
}
$UpdateDB2 = mysql_query("UPDATE `serverstatus` SET `Total` = $total WHERE `Total` = $Row[Total]");
echo "</td><td class=\"lista\">$Row[IP]</td><td class=\"lista\">$Row[Port]</td><td class=\"lista\">";
$accuracy = $uptime/$total;
if ($accuracy == 1) {
$accuracy = $accuracy * 100;
} else {
$accuracy = 100 - $uptime/$total;
}
echo "$accuracy%</td></tr>";
mysql_close($con);
}
} else {
$create = 'CREATE TABLE `serverstatus` (
`Name` VARCHAR (20) NOT NULL,
`IP` VARCHAR (50) NOT NULL,
`Port` VARCHAR (6) NOT NULL,
`Uptime` VARCHAR (999999) NOT NULL,
`Total` VARCHAR (999999) NOT NULL,
PRIMARY KEY (`Name`)
)';
mysql_query($create);
mysql_close($con);
}
echo "</table>";
}
}
?>
<p>
<b>Navigation</b><br />
<a href="Server_Status.php">Server List</a><br />
<a href="Server_Status.php?id=register">Register Server</a><br />
<? if($_SESSION['admin'] == 1) { echo "<a href=\"Server_Status.php?id=admin\">Administrator Panel</a><br />"; } else { echo "<a href=\"Server_Status.php?id=login\">Administrator Login</a><br />"; } ?>
<br />
<a href="http://validator.w3.org/check?uri=referer">
<img src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Transitional" height="31" width="88" />
</a>
</p>
</center>
</body>
</html>
Online image: [Only registered and activated users can see links. Click Here To Register...]
Offline image [Only registered and activated users can see links. Click Here To Register...]
Save them as online.gif and offline.gif
These were from the status Taharok made, and since I recently found them, I figured I'd use them. They are optional, so if you want to make your own, thats fine.
XHTML compliant :)
I'm still working on a proper formula for the uptime right now, if any of you want to hint me as to how its done, then go right ahead.
EDIT: Added the online and offline images