Reputation: 339
I would like to check the internet connection status using php. I tried this:
<?php
$connected = @fsockopen("www.google.com");
if ($connected){
$is_conn = true; //0
fclose($connected);
}else{
$is_conn = false; //1
}
echo (int)$is_conn;
?>
but it always returns true if I'm connected to the internal notwork! I need it to return false if I'm not online even if I'm connected to internal network.
Upvotes: 2
Views: 4816
Reputation: 5169
when you use @fsockopen as offline
if (!$sock = @fsockopen('www.google.com',80,$errorNum,$errorMessage)) {
echo "no connection";
echo $errorMessage;
}else{
echo "connection";
echo $errorMessage;
and print error message will be :
php_network_getaddresses: getaddrinfo failed: System error
Upvotes: 2
Reputation: 68556
Make use of this simple code to check whether internet is connected or not.
<?php
checkdnsrr('php.net') ? print 1: print 0; // " prints" 1 (If internet is ON)
Upvotes: 4