PDA

View Full Version : Script to detect website status



cr3ative
01-07-2005, 02:58 PM
Hello all,

I am looking for a script to detect the presence of a website (my home server), and to display text depending on whether the site is accessible or not.

The site is www.serberus.r8.org (Serberus is the network name of my system), that site will redirect to a longer url - it's a forwarder. You will probably get a page not found error as my server is usually off.

Because the server is rarely on, I need the script to check if it's online and write a link to the server if it is.

For example:
Server is online: Script writes "Serberus Online"
Server is offline: Script writes "Serberus Unavaliable"

If you have any questions at all, please feel free to ask as this script is highly desired.

Regards
cr3ative

edit: My 888th post :)

mwinter
01-07-2005, 06:29 PM
I am looking for a script to detect the presence of a website (my home server), and to display text depending on whether the site is accessible or not.With which language in particular? It should be possible with any decent server-side language and it's possible client-side, too (though only with certain browsers).

Mike

cr3ative
01-08-2005, 09:46 AM
The page which displays the text can't be on my server, so I would have to use my personal ISP space, and I'm not sure if they support many server-side technologies.

In an ideal situation, it would be made with javascript, but I'm sure I can ask my ISP to install a server-side application or two.

It's also worth noting that when it comes to server-side, I'm useless. I can tinker with .htaccess and mod.rewrite, but that's about it.

Thanks for your time
cr3ative

mwinter
01-08-2005, 12:02 PM
[...] it's possible client-side, tooUnfortunately, I was mistaken there. Whilst the XMLHttpRequest object can be used in some user agents to make HTTP requests to a remote server, there are security restrictions which mean requests can only go to the originating server for the current document.

So, you're back to only having a server-side option (well, unless you want to write - and sign[!] - a small Java applet). It can certainly be done with PHP (I'm looking for the best way, at the moment).

Mike

cr3ative
01-08-2005, 12:15 PM
Thank you very much for your time on this matter. I look forward to your findings.

I can also PM you my FTP access details if need be to set this up, as I am useless with PHP.

edit: My ISP space supports the following: Perl & SSI, PHP Version 4.1.2

Cheers
cr3ative

mwinter
01-08-2005, 01:14 PM
A basic implementation would be


<?php
$host = 'your domain name';
if($socket = fsockopen($host, 80, $errno, $errstr, 30)) {
echo 'The content you want to insert';
fclose($socket);
}
?>This simply attempts to connect to the address (name or IP) identified in host. If the connection succeeds, a string will be written into the location of the code. I take it that the IP address of your server is static?

The host name should be just a name: don't add a scheme (i.e. http://).

The last argument in the fsockopen call is the timeout before the connection aborts. You'll probably want lower than 30 (otherwise users might be waiting for thirty seconds before they get a complete response from your server), but don't make it too low otherwise you'll get a false-positive if network traffic is high. In my brief tests, my local Web server automatically cut out before the timeout (at around twenty two seconds) so you can't specify an excessive timeout period anyway.

I don't guarantee that this will work: though I didn't find any security restrictions mentioned, there might be some of which I'm not aware.

Mike

cr3ative
01-08-2005, 01:21 PM
edit: It works in my preliminary testing. Now to try to go live...

cr3ative

cr3ative
01-08-2005, 01:38 PM
Excellent!



<?php
$host = 'i-83-67-3-25.freedom2surf.net';
if($socket = fsockopen($host, 404, $errno, $errstr, 30)) {
echo 'Serberus is online!';
fclose($socket);
} else {
echo 'Serberus appears to be offline.';
}
?>

This example works, I've uploaded it at www.gerryhyh.f2s.com/paul/serberus.php - the only changes I had to make were to change the port to 404 (which serberus uses).

Thanks very much! :)
cr3ative

mwinter
01-08-2005, 01:59 PM
Thanks very much! :)You're welcome. :D

Mike