This is my setup:
I am running a cronjob in my home using a raspberry pi 3. Every 1 minute, this cronjob executes a php file that requires a functioning internet connection. To check if the internet is working at my home(finicky internet), I have modified and implemented the following function at the beginning of my file:
function is_connected_true_false()
{
$multiple_domains = array('www.example.com', 'www.google.com', 'www.bing.com');
$random_domain_used = $multiple_domains[rand(0,2)];
$connected = @fsockopen($random_domain_used, 80, $errno, $errstr, 10);
if($connected) {
$is_conn = true;
fclose($connected);
} else {
$is_conn = false; //action in connection failure
// Log error to the server
//$errno; // Error #
//$errstr; // Error as a string
}
return $is_conn;
}
This works so far, however, I believe I will face some problems in the future.
This cronjob runs the php file every 1 minute! This is 1440 website requests a day! from my home ip.
I've added at least 3 different domains, I do not wish to get my ip blacklisted. I choose this 3 domains because of their reliability and loading time.
It also seems to take some time to load the pages (sometimes a few seconds, finicky internet...).
There is probably higher bandwidth usage by opening an entire website rather than pinging 8.8.8.8.
Additional information: The php file essentially pulls information from other websites. I cannot use the (return code/exit status) when I call x website because of the long required timeout value. Otherwise, I'd use that.
Solution/Improvement?
Could I ping 8.8.8.8 server or any other reliable server instead of fully loading a website to check if internet is up? If so, how? Could I achieve the same result like in the current function?
I've tried 8.8.8.8 instead of the website domains but it does not seem to work. Not sure why.
Other: Please keep in mind, PHP hobbyist here. Although I did some research, I am not knowledgeable regarding network protocols, TCP, HTTP, ICMP, packages and all that kind of good stuff, Yet!
Source where I got the code: Determine in php script if connected to internet?
EDIT: -ochi has suggested that I buy or rent a host/VPS services instead. This could work. However, I believe that there is a better way. thanks.
EDIT2: I found out that a ping to a server usesdifferent protocol or port than an http request, how can I make sure port 80 is working? Should I just add more websites to my function array to disperse the traffic/requests?