PDA

View Full Version : How to make a simple php script to show server status.



dbeetle
01-12-10, 01:44 AM
I'm a noob to php, but I wanted to post my limited knowledge to help others out. If the code is crap, let me know what I did wrong, but I've tested it and it works for me.

How to make a simple php script to show server status if you don't have a ready-made public server website with the server status feature included.

First, make 2 icons showing "offline" and "online" status respectively. It is best to make these icons the exact same dimensions. Example: 20pixels x 20pixels
Name the files "serveron.gif" and "serveroff.gif" (you don't have to use a .gif, you can use a .jpg, .png, etc.)

Create a new folder named "serverstatus" or something similar. Put your 2 icons that you created into the folder. Then, create a file within the folder named "online.php" or something similar.

Copy/paste this code into the file.


$ip = "yoursite.com";
$onlinetext = "Server is On-Line" ;
$offlinetext = "Server is Off-Line" ;
$port = "80";

if(@fsockopen($ip,$port,$errno,$errstr,1)) {
echo"<a href='http://$ip' target='_blank'><img src='serveron.gif' width='20' height='20' border='0'></a> $onlinetext";
} else {
echo"<img src='serveroff.gif' width='20' height='20' border='0'> $offlinetext";
}
?>Replace "yoursite.com" with the url or ip address of the server.

Customize the "Server is On-line" and "Server is Off-line" messages to your liking.

Enter the correct port # (if unsure, leave at "80")

Save the file, and upload the folder to your host. Example: "www.yoursite.com/serverstatus/online.php".

Navigate to the url where you put your "online.php" file ie. "www.yoursite.com/serverstatus/online.php" and see it working!

To link to the php file from your web page, copy/paste the code below where you would like the status to be located in your web page.


<script><script language="JavaScript" type="text/javascript"
src="http://yoursite.com/serverstatus/online.php"</script>Replace "http://yoursite.com/serverstatus/online.php" with the location of the server status php file you created.

Save the file, and upload it.

That's it! You now have a live server status message with cool icons that you made all by yourself!:cool:

id820
07-05-11, 07:31 AM
You should link a demo for us, looks good anyway, im not sure if it works cuz i dont know php hehe.