Archives Dec. 2, 2012

Check your Internet connection | PHP Script Snippet

To check if your Internet connection is enabled, a simple solution is this function:


<?php
/**
 * Check Internet Connection.
 * 
 * @param string $sCheckHost Default: http://www.google.com
 * @return boolean
 */
function check_internet_connection($sCheckHost = 'http://www.google.com') 
{
    return (bool) @fopen($sCheckHost, 'r');
}
?>

A function is a little more advanced:


<?php
/**
 * Check Internet Connection.
 * 
 * @param string $sCheckHost Default: www.google.com
 * @return boolean
 */
function check_internet_connection($sCheckHost = 'www.google.com') 
{
    return (bool) @fsockopen($sCheckHost, 80, $iErrno, $sErrStr, 5);
}
?>

Example of use:


<?php
$bIsConnected = check_internet_connection();
$sText = ($bIsConnected) ? '<p style="color:green">You are currently connected to the Internet.</p>' : '<p style="color:red">You are ...

Continue reading

Previous day

Nov. 30, 2012

Next day

Dec. 7, 2012

Archives