Asked  7 Months ago    Answers:  5   Viewed   27 times

How can I check if I'm connected to the internet from my PHP script which is running on my dev machine?

I run the script to download a set of files (which may or may not exist) using wget. If I try the download without being connected, wget proceeds to the next one thinking the file is not present.


function is_connected()
    $connected = @fsockopen("", 80); 
                                        //website, port  (try 80 or 443)
    if ($connected){
        $is_conn = true; //action when connected
        $is_conn = false; //action in connection failure
    return $is_conn;

Wednesday, March 31, 2021
answered 7 Months ago

Thanks to Redirect Windows cmd stdout and stderr to a single file I can answer my own question. The trick is to redirect stderr to stdout:

$sCmdLine = "C:\Apache\php\php-cgi.exe -f ".__FILE__." 2>&1";
Saturday, May 29, 2021
answered 5 Months ago

If none of the above works for you, and you would still like to run under SSL, you could always write the file to disk and provide a download link, or you could prompt the user for an email address and email the file as an attachment. If you go the email route, PHPMailer has a fairly easy way of sending attachments. I'm not 100% for sure, but I think the file needs to be written to disk before it can be attached using PHPMailer, but you can always unlink the file immediately after send.

Saturday, May 29, 2021
answered 5 Months ago

There's an excellent answer to this question already, but since the OP is still having problems with this, here's an alternate solution. Please note that the array $a2s needs to be fully populated, as I only added two states to keep this short. Save the following to the file of your choice (eg: state.php):

$state = $_GET['state'];
echo convert_state($state);

function convert_state($key) {
    $a2s = array( 
    $array = (strlen($key) == 2 ? $a2s : array_flip($a2s));
    return $array[$key];

To run the above, you'd type one of the following in your browser (assuming your file is state.php):


The code is designed to work either way.

Saturday, May 29, 2021
answered 5 Months ago

1 Disconnecting internet cables is irrelevant. Use F8 to switch off/on internet in the emulator

2 In the first code this logic is wrong:

if(info != null || info.isConnected()) {  
              Log.v("NetworkInfo","Connected State");  

It should be:

if(info != null && info.isConnected()) {  
              Log.v("NetworkInfo","Connected State");  

If you use || then this is the result: a) when info is null, info.isConnected() will crash b) when info is not null, you will always show Connected State

However, even if you fix that, the code wont be completely correct (see 4)

3 In the second code, this logic is wrong:

 else if ( conMgr.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED
        ||  conMgr.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED) {

      Log.v("conMgr"," No Connection");


should be:

 else {

      Log.v("conMgr"," No Connection");


Why? Because there are more states than CONNECTED and DISCONNECTED.

However, even if you fix that, the code wont be completely correct (see 4)

4 This works on emulator and devices.

connected = (   conMgr.getActiveNetworkInfo() != null &&
            conMgr.getActiveNetworkInfo().isAvailable() &&
            conMgr.getActiveNetworkInfo().isConnected()   )

Note the use of isAvailable - without this isConnected can return TRUE when WIFI is disabled.

Hope this helps!

Sunday, September 19, 2021
Lasse Edsvik
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :