Check the internet connection using PHP function fsockopen

Here is a simple PHP function to check whether a machine is connected to internet or not. It uses a PHP function  fsockopen which initiates a socket connection to the given hostname at the specified port. If the connection is successful it will return true else false.

PHP code:

<?php 
//function to check if the local machine has internet connection 
function checkConnection() 
{ 
	//Initiates a socket connection to www.itechroom.com at port 80
	$conn = @fsockopen("www.itechroom.com", 80, $errno, $errstr, 30); 
	if ($conn)
	{ 
		$status = "Connection is OK";  
		fclose($conn);
	}
	else
	{
		$status = "NO Connection<br/>\n";
		$status .= "$errstr ($errno)"; 
	}
	return $status; 
}

echo checkConnection();
?> 
Be Sociable, Share!
You can leave a response, or trackback from your own site.

2 Responses to “Check the internet connection using PHP function fsockopen”

  1. Thanks for the script.. This is what I am exactly looking for… Solved my problem too :)
    Karthikeyan K recently posted..Product Comparison & Pricing Table using CSS3

  2. Ricky says:

    Thanks for the script. How if my web hosting do not enable fsockopen? Can we test connection with other method?
    Ricky recently posted..Perbedaan Situs Web, Web Hosting dan Web Desain

Leave a Reply

CommentLuv badge
 

Powered by WordPress