Determine in PHP Script If Connected to Internet

Determine in php script if connected to internet?

<?php
function is_connected()
{
$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;

}
?>

Determine in php script if connected to internet?

Just check if google.com is reachable:

<?php
if (!$sock = @fsockopen('www.google.com', 80, $num, $error, 5))
echo 'offline';
else
echo 'OK';
?>

php - check internet connect and DNS resolution

You can use fsockopen

Following example works well and tells you whether you are connected to internet or not

function is_connected() {
$connected = @fsockopen("www.google.com", 80); //website, port (try 80 or 443)
if ($connected){
fclose($connected);
return true;
}
return false;
}

Reference : https://stackoverflow.com/a/4860432/2975952

Check DNS resolves here

function is_site_alive(){
$response = null;
system("ping -c 1 google.com", $response);
if($response == 0){
return true;
}
return false;
}

Reference : https://stackoverflow.com/a/4860429/2975952

Run Php script constantly and if connected to internet as windows service which inserts data into xampp server

Hi Zaki Muhammad Mulla,

I did some testing myself, and I came up with the following piece of code

Running the code snippets here won't work because this is a sandbox and there is no access to the localstorage here.

Make sure to include Jquery in your code

<script  src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="crossorigin="anonymous"></script>

How to Check for an Initial Internet Connection in Chrome before loading a Web Page

A quite dirty hack with JSONP and jquery

using jsfiddle jsonp test:

(function testConnection() {
setTimeout(testConnection, 2000);
$.getJSON("http://jsfiddle.net/echo/jsonp/?callback=redirect&cb=?");
})()

function redirect() {
window.location = 'http://www.google.com'; // REDIRECT TARGET
}

first version:

(function testConnection() {
setTimeout(testConnection, 2000);
$.getJSON(
"http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
{format:'json'}
).done(function(result){
window.location = 'http://www.google.com'; // REDIRECT TARGET
});
})()

Works as expected, you might wanna have your own your own web server in order not to rely on a specific API

What is the best way to check internet connection

If you are using jQuery, you can just hook on the global error handler and lock up your application when an error occurs. The lock up screen could simply ask to try again.

$( document ).ajaxError(function() {
// lock your UI here
});

Also, once the UI is locked, you could execute a function that would ping your server in an Exponential Backoff fashion and automatically unlock the application on network restore.

Locking your app can easily be done with jQuery's blockUI plugin.

Example

(function ($) {
var locked = false;
var errorRetryCount = 0;
var blockUiOptions = { message: "Oops! Could not reach the server!" };

// change this function to adjust the exponential backoff delay
function backoff(n) {
return Math.pow(2, n) * 100;
}

$(function () {
$( document ).ajaxError(function () {
var req = this;

errorRetryCount += 1;

if (!locked) {
locked = true;
$.blockUI(blockUiOptions);
}

// retry to send the request...
setTimeout(function () { $.ajax(req); }, backoff(errorRetryCount));
}).ajaxSuccess(function () {
locked && $.unblockUI();
locked = false;
errorRetryCount = 0;
});
});
})(jQuery);

Note: You may not want to retry indefinitely your request upon network failure, and would want to quit retrying at some point. Since this is out of the scope of this question, I'll leave it as it is. However, you may take a look at this related question, which may help you sort this part out.



Related Topics



Leave a reply



Submit