How to use fsockopen to check internet on/off and not stop for error?

59 Views Asked by At

I am simply trying to see if I have internet or not but fsockopen always returns an error when internet is down that stops my script.

I am using the following code in a function

$starttime = microtime(true);
$file      = @fsockopen ($domain, 80);
$stoptime  = microtime(true);
$status    = 0;

if (is_resource($file)) {
    fclose($file);
    $status = ($stoptime - $starttime) * 1000;
    $status = floor($status);
} else {
    $status = -1;  // site is down
}
return $status;

...called in a script that reload the page every 10 sec and save the status with a timestamp in a file.

I tried $domain = @"ip address".

How can I avoid fsockopen to stops for error and be able to test it instead?

0

There are 0 best solutions below