php-general Digest 22 Dec 2009 12:41:39 -0000 Issue 6500

Topics (messages 300626 through 300626):

Re: Checking for internet connection.
        300626 by: Daniel Brown

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
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.

<?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.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!

--- End Message ---

Reply via email to