Check If Internet Connection Exists Or Not Using JavaScript & PHP

On 11/09/2017

Hi! Today let's see how to check if internet connection exists or not using javascript and php. The network connection may fail at times and many web applications require internet connection all the time. Hence, if there is no internet, these apps won't function properly. In such case, checking the status of the network through programming would be useful. There are many ways to do it, but here I'll show you the simplest method of all to detect whether user is online or offline both from client (using javascript) and server side (with PHP).

check if you are connected to internet or not

Check Internet Connection using JavaScript:

JavaScript has a function called navigator.onLine. This browser property can be used to determine if you are connected to the internet. It returns true or false depending on the status of the client's network and says whether the browser is online or offline easily.

Below is the JS function to verify connectivity to the Internet.

JS Function:

<script type="text/javascript">
function chkInternetStatus() {
    if(navigator.onLine) {
        alert("Hurray! You're online!!!");
    } else {
        alert("Oops! You're offline. Please check your network connection...");
    }
}
</script>

Function Usage:

Call the chkInternetStatus() function on button click and it will return the network status.

<button onclick="chkInternetStatus();">Check Internet Connection</button>
detect if browser is online or offline

Using SetInterval Function:

There is an even better method to check internet. You can use the setInterval() function to find the status of the network automatically at a certain time interval. Therefore, as soon as the network gets disconnected, the user will be notified immediately.

<script type="text/javascript">
setInterval(function(){
    var status = navigator.onLine ? 'You are online!' : 'You are offline!';
    console.log(status);
}, 5000);
</script>

Using the above methods you can check internet connection from the client side. Now let's see how to do it on the server side.

Check Online/Offline Status using PHP:

To check the internet connectivity in PHP, you can establish some socket connection and know the network status. If you are able to access specific web resource, it means you are connected to internet and not otherwise.

Here is the php snippet to detect if internet is working or not.

<?php
$host_name = 'www.google.com';
$port_no = '80';

$st = (bool)@fsockopen($host_name, $port_no, $err_no, $err_str, 10);
if ($st) {
    echo 'You are connected to internet.';
} else {
    echo 'Sorry! You are offline.';
}
?>

In the above code, we used a fsockopen() function. This initiates a socket connection to the given host (here it is 'google.com') and returns true if the server is reachable and false if it cannot.

Also Read:

Likewise you can check if the internet connection works or not in javascript and php. When things go wrong just because of net connection, it is better to notify users and let them fix it as soon as possible. This will also help improve the user experience. I hope you like this post. Please don't forget to share it in social networks.

No comments:

Post a Comment

Contact Form

Name

Email *

Message *