0

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?

  • Yes, cronjob has to run every minute. It will record if my internet is the one down – YoUser3139280 Oct 21 '18 at 01:20
  • your second question: no real life consequences, with the current function the server will log if my internet is down, the website does not work or if the website works. I don't see how paying for a host/VPS will teach me understand new code better. I guess if I wanted to I could pay someone to do it for me instead. – YoUser3139280 Oct 21 '18 at 01:27
  • no attitude, promised. I really need the frequency at 1 per minute. I believe is possible to maintain such frequency without issues. We are here to find out a way. Turning around the ping sounds good, I'm going to look into that. although, it involves another server no? not sure why you deleted your comments. :( – YoUser3139280 Oct 21 '18 at 01:48
  • Please read our [ask] page for hints on how improve this question – blurfus Oct 21 '18 at 01:49

1 Answers1

0

I wouldn't worry about it. A quick google search shows that google claims to serve 40k searches per second.

Additionally, google, bing, and all of the other giants use round robin DNS, and those IPs point to load balancers, so even if you have locally cached DNS info you should still toggle between each of the given servers and more.

ivan@darkstar:~$ nslookup www.google.com
Server:     192.168.1.2
Address:    192.168.1.2#53

Non-authoritative answer:
Name:   www.google.com
Address: 74.125.141.99
Name:   www.google.com
Address: 74.125.141.106
Name:   www.google.com
Address: 74.125.141.147
Name:   www.google.com
Address: 74.125.141.103
Name:   www.google.com
Address: 74.125.141.105
Name:   www.google.com
Address: 74.125.141.104
Name:   www.google.com
Address: 2607:f8b0:400c:c06::67

Your 1 request per minute is not even a drop in the ocean as far as they are concerned.

ivanivan
  • 1,999
  • 2
  • 9
  • 9