Define a script in PHP when connected to the Internet?

How can I check if I am connected to the Internet from my PHP script that runs on my dev machine (and not on the server somewhere)?

I am running a script to load a set of files (which may or may not exist) using wget. If I try to download wget without connecting, wget moves on to the next one, thinking that the file is missing, so I need to check before calling wget.

+3
source share
2 answers

Just check if google.com is available:

<?php
if (!$sock = @fsockopen('www.google.com', 80, $num, $error, 5))
echo 'offline';
else
echo 'OK';
?>
+6
source

A quick check is finding a hostname for a domain.

<?php
$ip = gethostbyname('www.google.com');
if($ip != 'www.google.com') {
  //connected!
} else {
  //not connected
}
?>
+1
source

Source: https://habr.com/ru/post/1789183/


All Articles