View Full Version : PHP remote server online status?
bluemicrobyte
01-09-2007, 1:04 AM
I have two web host servers. One for the site, and one for the large streaming media files. Occasionally the media file server will go down which will cause the videos on the main site to not load correctly.
I'm trying to set up a PHP script that can check if the second file server is online, and if it is NOT, then do something (like show a "videos unavailable" notification). Maybe there's some way I can have a PHP script check to see if it can load a small .txt file as a test to see if the remote server is responding?
Modred
01-09-2007, 7:49 AM
You could attempt to open a socket connection and then conclude if the server is or is not responding based on if the connection succeeds or fails.
<?php
// attempt to open a socket on port 80 (HTTP) to the site in question
// 30 is a timeout value, in seconds
// if the connection completely fails, fsockopen() will return 0 and put error
// code and messages into $errno and $errstr, respectively
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
// report the error if the connection failed.
echo "$errstr ($errno)<br />\n";
}
else {
// put whatever code for a good connection here
}
// close the socket
fclose($fp);
}
?>
I pulled most of that directly from the fsockopen() man page (http://us3.php.net/fsockopen), so hopefully it works. You would probably want a shorter timeout value, as I doubt you want your users to wait 30 seconds to find out if the media will load.
bluemicrobyte
01-09-2007, 9:16 PM
That looks like it should work, thanks Modred. I assume that a successful connection takes only a few milliseconds so it shouldn't increase page load times noticeably?
Modred
01-09-2007, 9:36 PM
Yeah, you'll want to change the last parameter to fsockopen() to something shorter than 30 seconds. I don't know exactly what a good timeout is for a TCP connection like this, but something around a half second should work. Maybe someone else around here has a better idea of a good timeout.
I would cache the result. Otherwise it's going to add overhead to every request. If the server is just running slow at one moment and takes more than half a second, it may show a false indication that the server is done when it isn't. If you set it higher (say 5 seconds) and the server really is down, it will add 5 seconds to every page load since the fsockopen is a synchronous operation (page won't continue loading until the function call is complete).
I would do something like this:
1. open cached status file
2. if the cached status is less than 5 minutes old, return the cached status vaue
3. otherwise, ping the server again and save the result, then return that
Modred
01-09-2007, 10:51 PM
Like I said, trust someone else with a little more experience to know a better way. =p
Like I said, trust someone else with a little more experience to know a better way. =p
Good call with fsockopen, though. I wasn't previously aware of any way to do a "ping" with a user defined timeout.
vBulletin® v3.7.2, Copyright ©2000-2008, Jelsoft Enterprises Ltd.