Determine in php script if connected to internet?

前端 未结 11 1025
轮回少年
轮回少年 2020-11-30 21:28

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 ma

11条回答
  •  萌比男神i
    2020-11-30 22:11

    There are various factors that determine internet connection. The interface state, for example. But, regardles of those, due to the nature of the net, proper configuration does not meen you have a working connection.

    So the best way is to try to download a file that you’re certain that exists. If you succeed, you may follow to next steps. If not, retry once and then fail.

    Try to pick one at the destination host. If it’s not possible, choose some major website like google or yahoo.

    Finally, just try checking the error code returned by wget. I bet those are different for 404-s and timeouts. You can use third parameter in exec call:

    string exec ( string $command [, array &$output [, int &$return_var ]] )

提交回复
热议问题