Jump to content

Script to check if website resolves, then return result


David Nelson

Recommended Posts

Hey,

 

On my site there are a lot of php-generated links, and sometimes they are broken. I've seen it before, but I was wondering if someone could point me in the right direction of a script that you could define a link to and have it check to see if the page exists/resolves, then return a result either "Working" or "Down" to the user.

 

Thanks everyone.

You could use cURL and check the HTTP response code:

 

<?php
$url = 'http://www.google.co.uk';
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_exec($ch);
echo curl_getinfo($ch,CURLINFO_HTTP_CODE);
?>

 

http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

With my method:

 

<?php
function checkurl($url){
    $ch = curl_init($url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
    curl_setopt($ch,CURLOPT_FAILONERROR,TRUE);
    if(curl_exec($ch) !== FALSE){
        return 'URL ok';
    }else{
        return 'Could not connect to site';
    }
    
}
echo checkurl('http://www.google.co.uk');
echo '<br />';
echo checkurl('http://www.madeupurl.com');
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.