How to Show if Server is Online or Offline?

Hi all

I have a web page on my ISP’s server that has a link to my own private server. My ISP’s server is always online, but my private server only sometimes.
I would like to somehow visualy indicate on my webpage whether my private server is online or off. Maybe a green light indicator for online and a red one for off.

My question therefore is this: is there some way using HTML or maybe Javascript that the link to my private server can be checked to establish if it is on or offline, and then to display the result accordingly as my page loads so that anyone visiting can instantly see my server’s status? I’m sure there must be a way, in fact I think Iv’e seen this sort of thing on the net somewhere.

If someone knows if this is possible or how to do it, would you please let me know - maybe even a snippet of code

Any help greatly appreciated
Dan Hawkes

I don’t think this this is possible with JavaScript as you need to operate on socket level.

One way to try to find out if a server is online is to try and open a socket connection to the server. If the connection fails, one can assume that the server is down (although the problem can be somewhere else too).

Check out this function and read all the user comments:

http://www.php.net/manual/en/function.fsockopen.php

Here’s a very simple demonstration:


<?php
if (fsockopen('www.sitepointforums.com', 80)){
   echo('The server in online');
} else{
   echo('The server in offline');
}
?>

The first parameter is the IP address of the server you want to check (in this case, www.sitepointforums.com, it could also be something like 123.123.123.123). The 80 is the port number (for HTTP), which can sometimes be different (21 for FTP etc.).

EDIT Typos + simplified the example.

Thanks itsyM

This looks a little complicated for me. What is it written in?
Someone on another forum posted this:-

Put a file up on your private server, for example foo.gif. Then, use simple code like this to check if the server is up or down:

code:--------------------------------------------------------------------------------<script type=“text/javascript”>
var url = “http://www.yourprivateserver.com/foo.gif”;
var img = new Image();
img.src = url;

img.onload = function()
{
	// If the server is up, do this.
	alert("Server is up!");
}

img.onerror = function()
{
	// If the server is down, do that.
	alert("Server is down!");
}

</script>--------------------------------------------------------------------------------

I replied with:-

Thanks for that code it looks great. However will it work with a password protected server - my private server (apache) is actualy running on my home PC, so for security it is always password protected, and that includes my root htdocs directory.
Do you think it would be to much of a security risk to have a small unprotected directory (presumably that would have to be the root - entry point) just to hold an image for the purposes of checking, and then to password protect the rest of the site?

What do you think, itsyM, would the security risk be too big?

Thanks in advance
Dan Hawkes

It is written in PHP, a widely supported scripting/programming language.

Someone on another forum posted this:-

Put a file up on your private server, for example foo.gif. Then, use simple code like this to check if the server is up or down:


<script type="text/javascript">
	var url = "http://www.yourprivateserver.com/foo.gif";
	var img = new Image();
	img.src = url;

	img.onload = function()
	{
		// If the server is up, do this.
		alert("Server is up!");
	}

	img.onerror = function()
	{
		// If the server is down, do that.
		alert("Server is down!");
	}
</script>

Yes, that works fine. However, it will fail to display the server status if the user has JavaScript turned off. Also, if something happens to the image you are linking to (you accidentally remove it, rename it or relocate it), the script will assume that the server is down.

The basic idea in that example is to try and link to an image on your server. If it fails one can assume that the server is down. A really simple (and not so glamorous) way to do this without any scripting is to make a normal link to an image on your page:


<img src="http://www.myserver.com/image/image.gif" />

Then write something like “If you can see this image my server is online. If you can´t see it my server is offline.”

:slight_smile:

However will it work with a password protected server - my private server (apache) is actualy running on my home PC, so for security it is always password protected, and that includes my root htdocs directory.

What kind of protection are we talking about? .htaccess?

Do you think it would be to much of a security risk to have a small unprotected directory (presumably that would have to be the root - entry point) just to hold an image for the purposes of checking, and then to password protect the rest of the site?

I think the image has to be inside a folder that is NOT protected. You could setup a separate folder for the image and leave the rest of the folders protected.

What do you think, itsyM, would the security risk be too big?

As long as you have your Apache configured correctly (security-wise), there should be no problems.