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
checking for broken external links
Moderator: General Moderators
-
michael_byl
- Forum Newbie
- Posts: 1
- Joined: Tue Aug 05, 2008 11:36 pm
-
devendra-m
- Forum Contributor
- Posts: 111
- Joined: Wed Sep 12, 2007 3:16 am
Re: checking for broken external links
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;
}
?>