Pages

Tuesday, July 16, 2013

Check for an internet connection using php

In writing a recent distributed db application (using AMP) I needed to find a way to check if the local machine had access to the internet, to be able to update the db at the master site. So I wrote this.

It uses fsockopen() to open a connection to a remote website, substitute yours in, and looks on ports 80 for http connections or 443 for SSL connections.

When called the function returns true id connected or false if not.




//-----------------------------------------------------------------------------
// is_connected function to check for internet connection 
//-----------------------------------------------------------------------------
<?php function is_connected() 
{ 
    
//check to see if the local machine is connected to the web 
    //uses sockets to open a connection to apisonline.com 
    
$connected = @fsockopen("www.some_domain.com", [80|443]); 
    if (
$connected){ 
        
$is_conn = true; 
        
fclose($connected); 
    }else{ 
        
$is_conn = false; 
    } 
    return 
$is_conn; 
    
}
//end is_connected function ?>


No comments:

Post a Comment

Thank you for your Comment....