a message appears when the connection drops

2

Colleagues.

I have a web-based system developed in PHP, but I would like a message to appear like Outlook and Facebook when the connection goes down. In PHP I did the following:

if(!$sock = fsockopen('www.google.com.br',80,$num,$error,5)){
  // Aqui apareceria a mensagem em bootstrap.
}

But it is not working. When the connection drops, that browser error page appears.

    
asked by anonymous 26.03.2016 / 22:49

1 answer

2

The only way to do what you want is on the client side and not on the server side.

Here's a Javascript example to detect whether the page is online or not at the time it is loaded:

if(navigator.onLine) { // true|false
    // ...
}

This other example runs the function if the Browser goes offline / online

//função a ser executada quando ficar online
function statusOnline() {
    //o seu código...
}

//função a ser executada quando ficar offline
function statusOffline() {
    //o seu código...
}

window.addEventListener('online',  statusOnline);
window.addEventListener('offline', statusOffline);

Another solution might be to use a library like offline.js

    
27.03.2016 / 22:02