0

I'm working on an web app which can go in offline mode. An ajax request checks every 5 seconds if a connection is available triggering the php fopen function as this :

if (fopen("http://google.com/", "r")) {
    return true;
}
return false;

This is my main question : Is it technically possible that the Apache/PHP get saturated and bring the server down ?

I've read all that is about on http://php.net/manual/fr/function.fopen.php but still can't get a clue about it ?

Kyobul
  • 759
  • 1
  • 7
  • 17
  • What do you mean with `[...]which can go in offline mode[...]`? Should your web app work on the client in offline mode (when the client has no internet connection)? If so then what do you want to test with this ajax request? If the ajax request is able to reach server (the php script) then the client if online, but why would you then do a `fopen("http://google.com/", "r")`? If the client has no internet connection, then the ajax call would not reach the php script, so `fopen("http://google.com/", "r")` would not be called. – t.niese Sep 20 '17 at 10:22
  • The offline mode is intended to provide a basic look and functionnality of the interface. Some data is cached with a manifest cache while some other data cannot be cached. The ajax request return "true" or "false" and append it to the element of the DOM. On every click action a JS checks if the connection status is active. If not, a cached data or limited functionnality is provided. – Kyobul Sep 20 '17 at 10:28
  • But it is for the case when the user of the WebApp has no internet connection, right? If so then the `fopen("http://google.com/", "r")` does not make any sense. PHP is a server side language that runs on your server. So your ajax request will only test if your server has a connection to `http://google.com/`, but not if the if the internet connection of the user is available. – t.niese Sep 20 '17 at 10:35
  • Yes, you are right and I understand the point you bring. But my question is : can this cycled fopen() get down, by any mean, my web server at given moment, because of its redundancy ? – Kyobul Sep 20 '17 at 10:39
  • 1
    Beside that it does not make any sense? Well if you have many users that use the WebApp and for each of those users you call every 5 second the domain `http://google.com` then you might get blocked or throttled by google or any other website because you do to many requests to them. And it is a bad habit to abuse a foreign site with permanent requests to just check a online connection exists, which might even result in a letter form your hoster. – t.niese Sep 20 '17 at 10:48
  • 1
    If you want to check if the client has an internet connection use [Online and offline events](https://developer.mozilla.org/en-US/docs/Online_and_offline_events) or [window.navigator.onLine](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine) which is supported by Chrome, Edge, Firefox, IE 8+ and Safari. – t.niese Sep 20 '17 at 10:48
  • Thank your for all your feedback, I appreciate that ! – Kyobul Sep 20 '17 at 11:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154890/discussion-between-kyobul-and-t-niese). – Kyobul Sep 20 '17 at 11:37

3 Answers3

0

I guess a better way to test if the internet connexion is up, is to ping a public IP like Google DNS 8.8.8.8.

check this post for the PHP code: Pinging an IP address using PHP and echoing the result

0

Your should Try:

<?php
    $connected = @fsockopen("www.example.com", 80); 
                                        //website, port  (try 80 or 443)
    if ($connected){
        $is_conn = true; //action when connected
        fclose($connected);
    }else{
        $is_conn = false; //action in connection failure
    }
    return $is_conn;
?>
Nikhil Parmar
  • 495
  • 3
  • 17
0

You can register the network connection event with javascript. When the notwork is on then pull the ajax request every 5s, while close the ajax request when the network connection off.

Register network connection event refer to How to manage network connection events and changes in availability (HTML)

LF00
  • 27,015
  • 29
  • 156
  • 295
  • This may be also helpful [how to detect the mobile connection is 2G/3G/WIFI using javascript](https://stackoverflow.com/q/25968020/6521116) and [https://stackoverflow.com/q/3181080/6521116](https://stackoverflow.com/q/3181080/6521116) – LF00 Sep 20 '17 at 10:26