Server Status Tutorial
Written by admin on June 15th, 2007 in PHP Tutorials.
Server Status is always a great help when you are running a hosting company or a business which needs to show it’s customers what is in service to use.
This tutorial will be showing you how to create a simple script to show your visitors what your server is doing.
Example:

To start with we need a php document.
I have called mine: status.php
Inside it we will start our script. This will be kept in a class so that it can be implemented into other scripts that you may use.
So first we will create a class.
- <span style="color: #000000"><span style="color: #0000bb"><?php</span></span>
// Create a new class with a name.
class serverSatus {Then create a function which will ping and display our results.
- <span style="color: #000000"><span style="color: #007700">function </span><span style="color: #0000bb">currentStatus</span><span style="color: #007700">(</span><span style="color: #0000bb">$url</span><span style="color: #007700">) {</span></span>
This function will carry the value of $url to display the status of multiple IP’s or URL’s.
Next we need to decide which ports/services we want to check are available on the server being pinged.
- <span style="color: #000000"><span style="color: #007700"></span><span style="color: #0000bb">$p </span><span style="color: #007700">= array(</span><span style="color: #dd0000">"HTTP" </span><span style="color: #007700">=> </span><span style="color: #dd0000">'80'</span><span style="color: #007700">, </span><span style="color: #dd0000">"FTP" </span><span style="color: #007700">=> </span><span style="color: #dd0000">'21'</span><span style="color: #007700">, </span><span style="color: #dd0000">"SSH" </span><span style="color: #007700">=> </span><span style="color: #dd0000">'22'</span><span style="color: #007700">, </span><span style="color: #dd0000">"MYSQL" </span><span style="color: #007700">=>
- </span><span style="color: #dd0000">'3306'</span><span style="color: #007700">, </span><span style="color: #dd0000">"CPANEL" </span><span style="color: #007700">=> </span><span style="color: #dd0000">'2082'</span><span style="color: #007700">, </span><span style="color: #dd0000">"WHM" </span><span style="color: #007700">=> </span><span style="color: #dd0000">'2086'</span><span style="color: #007700">, </span><span style="color: #dd0000">"POP3" </span><span style="color: #007700">=> </span><span style="color: #dd0000">'110'</span><span style="color: #007700">,</span><span style="color: #dd0000">
- "SMTP" </span><span style="color: #007700">=> </span><span style="color: #dd0000">'25'</span><span style="color: #007700">, </span><span style="color: #dd0000">"Ventrilo" </span><span style="color: #007700">=></span><span style="color: #dd0000"> 4510'</span><span style="color: #007700">, </span><span style="color: #dd0000">"Battlefield 2" </span><span style="color: #007700">=> </span><span style="color: #dd0000">'16567'</span><span style="color: #007700">); </span></span>
Above the array is holding all the port numbers of a normal servers services. Along with them are defined the services abbreviated name.
More ports can be added.
Next we need to get the status of each service one by one. For this we use a
- <span style="color: #000000"><span style="color: #007700"> echo </span><span style="color: #dd0000">'<table>'</span><span style="color: #007700">;</span></span>
foreach($p as $a => $b) { I have added a table tag which will house our results. This foreach statement is saying, foreach part of the array turn the values into the variables a and b.
a is the Service name.
b is the Service port.
So now that we have the port and name separated we can run a pinging function on the given URL or IP.
- <span style="color: #000000"><span style="color: #0000bb">$x </span><span style="color: #007700">= @</span><span style="color: #0000bb">fsockopen</span><span style="color: #007700">(</span><span style="color: #0000bb">$url</span><span style="color: #007700">, </span><span style="color: #0000bb">$b</span><span style="color: #007700">, </span><span style="color: #0000bb">$errno</span><span style="color: #007700">, </span><span style="color: #0000bb">$errstr</span><span style="color: #007700">, </span><span style="color: #0000bb">30</span><span style="color: #007700">); </span></span>
Our pinging query is kept within a variable of x.
30 is the number of milliseconds until the query times out. When the query times out it will return
Next we need to determine whether the query returned
So we run an
- <span style="color: #000000"><span style="color: #007700"> if(</span><span style="color: #0000bb">$x</span><span style="color: #007700">) {</span></span>
echo ‘<tr><td>’.$a.‘</td><td><font color=”green”> is currently Online
</font></td></tr>’;
} else {
echo ‘<tr><td>’.$a.‘</td><td><font color=”red”> is currently Offline
</font></td></tr>’;
}
The
Service Name is currently
If it is false the second echo statement will be parsed and will give the following message:
Service Name is currently
In this instance Service Name is defined as $a as set in the foreach statement.
Now we end our foreach statement. Then end our table with the </table> tag.
- <span style="color: #000000"><span style="color: #007700"> }</span></span>
echo ‘</table>’;Then end our function.
- <span style="color: #000000"><span style="color: #007700">} </span></span>
Finally we need to close our class and define it to a variable and end the php tags.
- <span style="color: #000000"><span style="color: #007700">}
- </span><span style="color: #0000bb">$Status </span><span style="color: #007700">= new </span><span style="color: #0000bb">serverSatus</span><span style="color: #007700">;
- </span><span style="color: #0000bb">?></span></span>
Now we should end up with the following code in status.php
When you need this class just include this page.e.g.
- <span style="color: #000000"><span style="color: #0000bb">include</span></span>
- <span style="color: #000000"><span style="color: #dd0000">('status.php');</span></span>
To actually use this script and display the results we need to call our created currentStatus function. To do this we do the following.
- <span style="color: #000000"><span style="color: #007700">
- </span><span style="color: #0000bb">$Status</span><span style="color: #007700">-></span><span style="color: #0000bb">currentStatus</span><span style="color: #007700">(</span><span style="color: #dd0000">"http://www.pdstudios.net"</span><span style="color: #007700">);
- </span></span>
OR
- <span style="color: #000000"><span style="color: #007700">
- </span><span style="color: #0000bb">$Status</span><span style="color: #007700">-></span><span style="color: #0000bb">currentStatus</span><span style="color: #007700">(</span><span style="color: #dd0000">"66.45.242.210"</span><span style="color: #007700">);</span></span>
An Example of the script in action can be found here: Server Status Example
Within the quotes you can give an IP address or a URL.
I hope this has helped your understanding and this comes in helpful with any projects you may be developing. If you would like and help please reply here.