Re: Checking for internet connection.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, Dec 19, 2009 at 19:13, Angus Mann <angusmann@xxxxxxxxx> 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@xxxxxxxxxxxx || danbrown@xxxxxxx
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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux