Determine in php script if connected to internet?

前端 未结 11 1043
轮回少年
轮回少年 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条回答
  •  盖世英雄少女心
    2020-11-30 21:56

    This code was failing in laravel 4.2 php framework with an internal server 500 error:

    
    

    Which I didn't want to stress myself to figure that out, hence I tried this code and it worked for me:

    function is_connected()
    {
      $connected = fopen("http://www.google.com:80/","r");
      if($connected)
      {
         return true;
      } else {
       return false;
      }
    
    } 
    

    Please note that: This is based upon the assumption that the connection to google.com is less prone to failure.

提交回复
热议问题