checking for broken external links

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michael_byl
Forum Newbie
Posts: 1
Joined: Tue Aug 05, 2008 11:36 pm

checking for broken external links

Post 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
devendra-m
Forum Contributor
Posts: 111
Joined: Wed Sep 12, 2007 3:16 am

Re: checking for broken external links

Post 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;
}
?>
Post Reply