appear a message when connection drops

Asked

Viewed 38 times

2

Colleagues.

I have a web-based system developed in PHP, but I would like a message to appear as it occurs in Outlook and Facebook when the connection falls. In PHP I did as follows:

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

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

  • php is a language that runs on the server side and is not on the aware side. So if the connection drops, of course it cuts the connection to the server where this php runs.

1 answer

2


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

Here is an example in Javascript to detect whether or not the page is online 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 may be to use a Libreria such as offline js.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.