PDA

View Full Version : Online/Offline script help



cursed
12-17-2006, 05:00 AM
I want a simple php script to show if a website is offline or online.. however, i dont want it to show

Warning: file_get_contents( http://www.svyt.com/proxy.html) [function.file-get-contents]: failed to open stream: No such file or directory in /home/user/public_html/proxy/status.php on line 6


I just want it to show:
Offline
heres my code:


<?php if(strpos(file_get_contents(' http://www.svyt.com/proxy.html'), 'advanced') !== false)
echo('<font color="green">Online</font>');
else
echo('<font color="red">Offline</font>'); ?>


Note: svyt.com/proxy.html should contain the text advanced - no need to change any part of that.

djr33
12-17-2006, 07:46 AM
@file_get_contents(' ht.......

The @ symbol suppresses errors.

Twey
12-17-2006, 06:08 PM
That's a rather inefficient way of doing it. The simplest would be to just connect to the remote server:
<?php
$online = false;
if($f = fsockopen('svyt.com', 80)) {
$online = true;
fclose($f);
}
?>However, this won't catch if the server is up but the site not functioning. You could use an HEAD request for that:
<?php
$online = false;
if($f = fsockopen('svyt.com', 80)) {
$request = <<<END
HEAD /proxy.html HTTP/1.1
Host: www.svyt.com
Connection: Close

END;
fwrite($f, $request);
$response = '';
while(!feof($f) && strpos($response, "\n") === false)
$response .= fread($f, 1);
$response = explode(' ', $response);
$response = $response[1];
if($reponse[0] < 4 && $reponse[0] > 1)
$online = true;
fclose($f);
}
?>

cursed
12-22-2006, 07:38 PM
thanks, djr33, it worked perfectly.
Twey, ill try your idea later, would it work if there was a custom 404 page? or a 404 page got redirected into a different page?

Twey
12-22-2006, 07:42 PM
Indeed.

boxxertrumps
12-22-2006, 11:08 PM
I have Something similar, except i use an Iframe that points to a text file on my server.

too Bad freewebs doesn't support php.

djr33
12-23-2006, 03:14 AM
Freewebs is just evil. There really are better free hosts out there :)