Page 1 of 1

checking for broken external links

Posted: Wed Aug 06, 2008 12:07 am
by michael_byl
I am developing a portal site that maintains a database of external links. I would like to be able to check, using php, whether the links are still active. Is there any way of doing this?

I am after a function of the form:

boolean checkLink(string url)

which returns true if the link is active and false if it is not

Re: checking for broken external links

Posted: Wed Aug 06, 2008 3:24 am
by devendra-m

Code: Select all

function checkLink()
{
    $url     = "www.example.com/index.php";
    $host    = "www.example.com";   
    $header  = "GET http://{$url}/ HTTP/1.0 \r\n";
    $header .= "Host: {$host}\r\n";
    $header .= "Connection: Close\r\n\r\n";
    
    
    $fp = fsockopen ($host, 80, $errno, $errstr, 30);
    fputs ($fp, $header);
 
        
    while (!feof($fp)) 
    {
        $response = fgets ($fp, 1024);
        break;
 
    }
    
    fclose($fp);
      if(preg_match('/ok/i',$response))
        return true;
      else
                 return false;
}
?>