Display server status (on or off) on web page
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 wil开发者_JS百科l 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.
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
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.
You can use the PEAR-Package "NET_Ping" to ping your server.
精彩评论