views:

134

answers:

3

Curious. I'm starting to broadcast high school football games online, and it uses a program to broadcast the feeds off my computer. However, when I shut the program down or the computer down, the server goes offline and guests won't be able to access the feeds.

Is there any kind of code out there that I can post onto my website that will indicate to my guests whether the server is on or off? I would figure it would be a simple code, a php script or something that periodically checks to see if a site is on line and then displays ON or OFF.

+1  A: 

Two options here, the first is you can request a response from the remote server and if it doesn't exist (after some timeout) claim that it's offline. You can cache this if response times can be slower then the time users will want to wait, or shove this into an AJAX request that can then remove the video player, display the notice, etc. on failure.

The other alternative is to check for feed failure and display a server offline message.

Aea
AJAX is, of course, restricted by domain policy.
Matchu
If the website is example.com, and the feed server is feed.example.com, though, you're good to go.
Matchu
I wasn't verbose enough in my reference to AJAX. There's no reason to have the AJAX script ping the server directly, this can be (and SHOULD ) be accomplished through a proxy "ping" script on the same domain.
Aea
+5  A: 

EDIT: use @fsockopen or you'll get a Cannot connect to the server warning which you can ignore since that is what you're actually trying to check.

$ping = @fsockopen('myURL or IP', 80, $errno, $errstr, 10);
if (!$ping) // site is down

E.g:

<?php
$URL = 'randomurlfortestingpurposes.com';
$ping = @fsockopen ($URL, 80, $errno, $errstr, 10);
(!$ping) ? $status = $URL.' is down' : $status = $URL.' is up';

echo $status.'<br>';

$URL = 'google.com';
$ping = @fsockopen ($URL, 80, $errno, $errstr, 10);
(!$ping) ? $status = $URL.' is down' : $status = $URL.' is up';

echo $status;
?>

Outputs

randomurlfortestingpurposes.com is down
google.com is up
Ben
Hmm...I tried that PHP script. I set the IP address in the URL section and changed the port to 86 (that's the correct port) and when I upload my PHP file to my server, it gives me the Offline notice while it has Google listed as Online.I can confirm the server is online. I checked http://ping.eu/port-chk/ and it confirms the IP/Port is operational. I tried numerous other PHP scripts similar to the one above, but they still give me an Offline error.Any idea what I'm doing wrong?
Daniel
A: 

You can use the PEAR-Package "NET_Ping" to ping your server.

Blauesocke