Forum Moderators: coopster & phranque

Message Too Old, No Replies

How to add "detect other server online" function in html page?

         

Teresa_Teng

8:18 am on Mar 18, 2003 (gmt 0)

10+ Year Member



I want to add a function in html page for automatic detecting other computer / server ( for example: ftp://192.168.2.21 ) online or not when the homepage onload. Language something like that:

if server_online = true
document.write ("server is ON line")
else
document.write ("server is OFF line")
endif

Can anybody present me the detail correct language? Thanks.

Teresa

Allen

8:55 am on Mar 18, 2003 (gmt 0)

10+ Year Member



There's two ways.

1. You have an image on the other server. If the image displays ,the server is online.

2. You use a server side script that pings the other server.

Allen

Teresa_Teng

9:12 am on Mar 18, 2003 (gmt 0)

10+ Year Member



I've tried the first one before, but it seems imperfection because it shows a red 'x' inside a little white frame if the image cannot shown while the server is offline.

tedster

10:38 am on Mar 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since AFAIK javascript does not offer you a direct way to ping another server, and the image "solution" is not really a solution for you, I think you'll need a server side solution - perl or php, whatever CGI potential you've got going.

So I'm moving this post from the HTML and Browsers forum over to the Server Side Scripting forum - I'll bet we can have an answer for you in no time.

nosanity

6:33 pm on Mar 18, 2003 (gmt 0)

10+ Year Member



Well, in PHP I would use something like this:

$fp = fsockopen ("www.example.com", 80, $errno, $errstr, 30);
if (!$fp)
{
echo "Server is offline.";
} else {
echo "Server is online.";
fclose($fp);
}

noSanity

PS: Can we get a Perl example for Teresa_Teng?

dive into perl

7:33 pm on Mar 18, 2003 (gmt 0)

10+ Year Member



Perl example :-

You can do this by using the LWP::UserAgent.pm [search.cpan.org] module along with HTTP::Status.


use LWP::UserAgent;
use HTTP::Status;

my $ua = new LWP::UserAgent;

# define a User Agent and Timeout #
$ua->agent('My server cheker');
$ua->timeout(20);

# make the request
my $req = new HTTP::Request( GET => 'http://www.somesite.tld');

# check if server responds
my $response = $ua->request($req);

if ($response->is_success) {
print "Server is online";
} else {
print "Server is offline";
}

Teresa_Teng

1:54 am on Mar 19, 2003 (gmt 0)

10+ Year Member



Hi, dive into perl,

Thanks for your help. But, I'm sorry, I'm not familiar with Perl.
Is it possible to convert to html or embedded hyperlink? Thanks.

Teresa

GeorgeGG

3:00 am on Mar 19, 2003 (gmt 0)

10+ Year Member



$fp = fsockopen ("www.example.com", 80, $errno, $errstr, 30);

$ua->timeout(20);

Why a 30 or 20 second timeout?
Wouldn't that mean if the server is down,
the script/s wouldn't show 'Server is offline'
until the timeout?

GeorgeGG

<EDITED>

Try this

file on-off.htm

<html><head><title>OnLine/OffLine</title></head><body>
<script TYPE="text/javascript">
var on_line = "No";
</script>

<script type="text/JavaScript" src="http://my_domain.com/on-line.js"></script>

<script TYPE="text/javascript">
if (on_line!= "No")
document.write("Server is online<br>");
else
document.write("Server is offline<br>");
</script>
</body></html>

file on-line.js

on_line = "Yes";

Still have problem with the time out if server is down...

I use a 1 second timeout for head requests/ftp checks and
other off server checks.

Teresa_Teng

3:53 pm on Mar 19, 2003 (gmt 0)

10+ Year Member



Hi, GeorgeGG,

I've tried your on-off.htm, but it seems cannot run automatically.
If var on_line = "No"; it had to show offline and
if var on_line = "Yes"; it had to show online no matter how the server is on or off.

Teresa

nosanity

5:51 pm on Mar 19, 2003 (gmt 0)

10+ Year Member



Through multiple attempts using javascript, I found that if the remote page is offline, the page loading is quite slow. Once I dropped the php timeout to 5 seconds, I had that script working great. It tests the remote page for 5 seconds then aborts. Once aborted, the script knows the server is offline. If it did connect, the script knows the server is online.

noSanity

GeorgeGG

1:14 am on Mar 20, 2003 (gmt 0)

10+ Year Member



Teresa_Teng
This is a bad choice:

if (on_line!= "No")
document.write("Server is online<br>");
else
document.write("Server is offline<br>");

If 'on_line' is a NULL then it would say server online.

Change to


if (on_line!= "Yes")
document.write("Server is offline<br>");
else
document.write("Server is online<br>");

Also do not change the 'var on_line = "No";'

nosanity

using javascript,
if the remote page is offline, the page loading is quite slow.

That is what's so bad about those JavaScript track/loggers
that are available...

You can probably drop the time down to 1 or 2 seconds,
I started off using 5 seconds on my sockets and finally settled on
1 second on everything on my pages and 10 for whois...

GeorgeGG

nosanity

6:28 am on Mar 20, 2003 (gmt 0)

10+ Year Member



Woohoo... got it down to 2 seconds, but it had problems with 1 second. It just wasn't enough time... 2 seconds works great.

YAY!

Now I just need to find a use for it on a new project. Hmmmm......

noSanity

Teresa_Teng

11:49 pm on Mar 20, 2003 (gmt 0)

10+ Year Member



GeorgeGG,

I've changed as your instruction, but the status is same:
Cannot show online or offline automatically.

I've checked server with src="ftp://server/teng/online.gif"
Server side feedback is normal:

> logged in anonymously with login/pass : anonymous/IEUser@
> 230 User teresa logged in.
> TYPE I
> 200 Type set to I.
> PASV
> 227 Entering Passive Mode (203,80,102,86,72,198).
> SIZE /teng/online.gif
> 550 No such file or directory.
> RETR /teng/online.gif
> asked to download 'e:\online.gif' --> Access allowed.
> 150 Data connection accepted from 203.80.102.86:1226; transfer starting for online.gif (2673 bytes).
> started downloading 'e:\online.gif'.
> finished downloading 'e:\online.gif' - (00:00:01 - 2.610 KB - 2.610 KBytes/s)
> 226 Transfer ok
> ABOR
> 226 ABOR command successful.

My be something wrong at my side?

Teresa

gethan

2:03 am on Mar 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I like the javascript solutions presented here - though to improve perceived performance you could put the detection in an onload event - the rest of the body will load first.
eg. <body onload='detect_server()'>

Then make the results change the innerhtml for a results area. The contents of that area before load could be 'Testing Remote Server'

Hope this is clear, haven't tested the ideas but am pretty sure they would work nicely.

Teresa_Teng

2:43 pm on Mar 21, 2003 (gmt 0)

10+ Year Member



GeorgeGG,

Thank you very much for your detail explanation, now the on-off.htm run smoothly. Thanks a lot.

Teresa