Click to See Complete Forum and Search --> : How to Show if Server is Online or Offline?


dandino
11-15-2003, 07:46 AM
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

fredmv
11-15-2003, 07:58 AM
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>I hope that helps you out.

Khalid Ali
11-15-2003, 08:06 AM
Crossposting is considered a rude thing to do,please refrain from repeating it.

dandino
11-15-2003, 08:21 AM
oops sorry about the cross posting, won't do it again :(

fredmv:

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. Can an image still be checked?

Dan Hawkes

fredmv
11-15-2003, 08:23 AM
Unfortunately, no. I don't believe it will allow it without a correct username and password. Maybe you could add something to your .htaccess that would allow connections from that specific machine?

dandino
11-15-2003, 08:35 AM
Hello again fredmv,

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?

fredmv
11-15-2003, 08:58 AM
Server security really isn't something I know too much about, but I suppose you could try it for a little, if someone breaks the security, use another method.

dandino
11-15-2003, 09:18 AM
Excuse my ignorance fredmv but in that snippet of code that you posted is the line:

"img.onload = function()"

exactly as I should write it, or is function() just an example of a function that I have to write? Sorry but havn'nt coded in javascript for a while.

Thanks again
Dan Hawkes

fredmv
11-15-2003, 09:26 AM
No problem. The way I wrote it is how it should be kept, it's called an annonymous function (a function with no name).

dandino
11-15-2003, 09:41 AM
ok, thanks very much for your time fredmv :)

fredmv
11-15-2003, 09:43 AM
You're welcome. :cool: