Re: Checking for internet connection.

From: Date: Mon, 21 Dec 2009 17:27:53 +0000
Subject: Re: Checking for internet connection.
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-300626@lists.php.net to get a copy of this message
On Sat, Dec 19, 2009 at 19:13, Angus Mann <angusmann@pobox.com> wrote: > Hi all. > > I'w writing a PHP app that is designed to run over a LAN, so internet > connection for the server is not really essential. Some users may > deliberately not connect it to the internet as a security precaution. > > But I'd like the app to make use of an internet connection if it exists to > check for an update, and notify the user. > > Is there a simple way for a PHP script to check if it has an internet > connection? If it's running on Linux, this will work. For other OS'es, you may have to tweak it a bit. <?php $ip = '24.254.254.1'; // This is a bogus address. Replace it with yours. exec('ping -c 1 -w 3 '.$ip,$ret,$err); if($err) die('Internet connection unavailable.'); ?> This executes a system call to the PING utility, which then sends a single packet with a deadline of 3 seconds to the address. If it causes anything but a 0 return on STDERR, it dies with the message "Internet connection unavailable." Don't use name-based lookups unless you absolutely have to in this case. There are more points of failure and bottlenecking, which can make your code run really slow or fail completely. -- </Daniel P. Brown> daniel.brown@parasane.net || danbrown@php.net http://www.parasane.net/ || http://www.pilotpig.net/ Looking for hosting or dedicated servers? Ask me how we can fit your budget!

« previous php.general (#300626) next »