On Sat, Dec 19, 2009 at 19:13, Angus Mann <angusm...@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.

$ip = ''; // 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.br...@parasane.net || danbr...@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Looking for hosting or dedicated servers?  Ask me how we can fit your budget!

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to