Find out if a website is on or off

Asked

Viewed 2,982 times

3

Is there any PHP function to find out if an IP address (meaning website) is in the air or not?

I found this function on the WEB but she did not satisfy me.... it takes too long to load.

function curl_info($url){
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_HEADER, 1);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );

    $content = curl_exec( $ch );
    $info = curl_getinfo( $ch );

    return $info;
}

$site = 'http://www.locaweb.com.br';
$info = curl_info( $site );
if( $info['http_code']==200 ) {
    echo '<u>'.$site . '</u> - <strong>está no ar!!</strong><br />';
} else {
    echo '<u>'.$site . '</u> - está fora do ar<br />';
}

  • What do you mean if an IP is online? Do you want to check what exactly? A service or user?

  • From what I understand you want to check if a site is "online" right? If that’s the case I think this is really the best solution. It takes a long time? How long in seconds? Oh one more thing the site may be online but on the page be "in maintenance", I do not know if it is relevant, but it is the caveat.

  • worse than it takes rs, for example, testing 'https://www.google.com' this function takes 21 seconds to finish... I don’t know if I’m doing anything wrong, or if this is right

  • The solution could be in Javascript?

  • could be yes, but I find it difficult to create such a solution

  • Quick question: wouldn’t it be easier to use a service like Pingdom? (unless you actually want to develop a script for your purpose)

  • no no, the purpose is another....

Show 2 more comments

3 answers

6

1) Using fsockopen, note the format of the URL

if( fsockopen( 'www.locaweb.com.br' , 80 , $errno , $errstr , 30 ) ){
    echo 'site online!';
} else {
    echo 'site offline.';
}

2) Using checkdnsrr: Checks DNS records that match a given host name or Internet IP address.

checkdnsrr( 'google.com' ) // TRUE
checkdnsrr( 'yaruuu.com' ) // FALSE

3) Using Curl

$ch = curl_init('http://www.locaweb.com.br');  
curl_setopt($ch, CURLOPT_TIMEOUT, 5);  
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
$data = curl_exec($ch);  
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);  
curl_close($ch);  

if( $httpcode >= 200 && $httpcode < 300 ){  
    echo 'site online!';
} else {
    echo 'site offline.';
}

In the case of CURL, the URL http://www.locaweb.com.br returns a status 301 redirect to the URL http://www.locaweb.com.br/default.html, we may accept the site as ONLINE. In the above example it does not consider redirect status as ONLINE, but you can change and accept these cases:

status redirect:

300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found', // 1.1
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
// 306 is deprecated but reserved
307 => 'Temporary Redirect',

It is up to you to define the status that will be valid as ONLINE.

1

I had the same problem and changed the script.
The "responsibility" of getting data from the page passed to another function.

Function to check only if the given link returns any response:

//verifica se o link informado retorna alguma resposta
function isUrl($_sUrl){
    $bRet = false;

    $cl = curl_init($_sUrl);
    curl_setopt($cl,CURLOPT_VERBOSE, true);
    curl_setopt($cl,CURLOPT_CONNECTTIMEOUT,1);
    curl_setopt($cl,CURLOPT_HEADER,true);
    curl_setopt($cl,CURLOPT_NOBODY,true);
    curl_setopt($cl,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($cl,CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($cl);
    curl_close($cl);
    if($response) $bRet = true;

    return $bRet;
}

In my case, I check if there is any video on the page:

function getVideo($_sUrl){
    $sVideo = '';
    //necessário a extensão php_openssl activa para ulr's com https
    $html = new DOMDocument();
    @$html->loadHTML(file_get_contents($_sUrl));
    $aMeta = $html->getElementsByTagName('meta');
    foreach($html->getElementsByTagName('meta') as $meta) {
        if($meta->getAttribute('property')=='og:video'){
            $sVideo = $meta->getAttribute('content');
            break;
        }
    }

    return $sVideo;
 }

To use the code above:

$sUrl = 'https://www.youtube.com/watch?v=OZ5gVkSjC8k';
if(isUrl($sUrl))
    $sVideo = getVideo($sUrl);
  • worked non mano... I used like this: $Surl = 'https://www.google.com/'; isUrl($Surl); And don’t give me any return =x

  • @Marcelobonifazio you have to adapt your need, see the example I posted, check first if there is any server response (function isUrl) and then look for the meta tag 'og:video' (function getVideo), which in the case of google.com does not exist, soon the return of the getVideo function is empty.

  • @Marcelobonifazio the function isUrl returns true/false, so you must do an "if", for example, echo isUrl('sua_url') ? "online" : "offline";

1

I’m no expert on the Web part, but I believe that if you run a Get at the address and return successfully, the address is "online"

var urlValida = '/echo/html/';
var urlInvalida =  '/NaoExiste/html/';

$.ajax({    
    type: 'GET',
    url: urlValida,
    success: function (data) {
        alert(urlValida + ' OnLine');
    }
}).error( function () {
        alert(urlValida + 'OffLine');
});

$.ajax({
    type: 'GET',
    url: urlInvalida,
    success: function (data) {
        alert(urlInvalida + ' OnLine');
    }
}).error( function () {
        alert(urlInvalida + 'OffLine');
});

The code can be tested in this example in jsFiddle

  • I think Voce didn’t understand old kk I don’t want to search for files within my project and etc.... I want to check external sites, other server sites, like cmd ping command

  • for example... ping www.google.com returns to me that the packages were sent and returned successfully, that is, the site is 'visible', online

  • 1

    @Marcelobonifazio But that’s what I did, I’m disregarding the data returned. I’m just validating whether or not there was success in the remote call. I thought about using HEAD or instead of GET but I don’t know if every HTTP server responds to HEAD. Take the example of Jsfiddle, I just couldn’t use real Urls because there is a lock, but if you run the code on your page, with the urls posted the result will probably be the same

  • Well, in this example, var urlValida = 'www.google.com'; Daria online correct?

  • Yes, but outside Jsfiddle because it does not allow external calls. and http://www.googlelalalalala.com.br daria offline

Browser other questions tagged

You are not signed in. Login or sign up in order to post.