# script to check for dead links in database



## jorre (Oct 27, 2005)

hi all, 

I have a question for the php or other scripting lovers around here...

since some time I've been looking for a script that can check if any of my database links are dead...

this means: 
I have a mysql database and it's filled with links. I need to check all of these links to see if there are any dead ones between them, so that I can give my visitors a good clean database without dead links ...

Well, until now, I came up with the following script... the problem is, that good sites are reported as being dead for the reason (as far as I know and think) that the URL's are being forwarded. 

If anyone knows how I can get around this, OR has another way of automatically checking and cleaning out my link database, it would be greatly appreciated:

well: here we go:





<?php
#This script has been developped by Nenad Motika [[email protected]]
#Please feel free to use it and to contact me for any reason

$linkovi = fopen("links.html","a");
fwrite($linkovi,"<!doctype html public \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
fwrite($linkovi,"<html>\n<head>\n<title>Link checker results</title>\n");
fwrite($linkovi,"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-2\">");
fwrite($linkovi,"</head>\n <body bgcolor=white>");
fclose ($linkovi);

$db = mysql_connect("localhost", "root", "");
mysql_select_db("database",$db);
$result = mysql_query("SELECT * FROM tbllinks order by link_id asc;",$db);
$n = mysql_num_rows($result);
for ($i=0;$i<$n;$i++){
$linkovi = fopen("links.html","a");
$urlid=mysql_result($result,$i,"link_id");
//$urlid = 986;
$URL=mysql_result($result,$i,"link_url");
//$URL = "http://www.pakfriet.be/";
$URLInfo = parse_url($URL); 



//$DocumentPath = ($URLInfo["path"])?$URLInfo["path"]:"/"; 
if (!isset($URLInfo["path"]))
{
$URLInfo["path"]= "/";
}
$DocumentPath = $URLInfo["path"];


//print_r($URLInfo);
//exit;

if (isset($URLInfo["query"]))
$DocumentPath = $DocumentPath."?".$URLInfo["query"]; 
//$DocumentPath = ($URLInfo["path"]);
else
unset($URLInfo["query"]);
$conn = fsockopen($URLInfo["host"], 80, &$errno, $errstr, 30); 
if ($conn) 
{ 
$host=$URLInfo["host"];
fwrite ($conn, "HEAD ".$DocumentPath." HTTP/1.0\r\nHost: $host\r\n\r\n"); 
$sadrzaj= fgets($conn,22);
$ok="200 OK";
$status=strrchr($sadrzaj,$ok);
$greska=substr($sadrzaj,9,3);
if (!$status) {fwrite($linkovi,"<a href=\"update.php?I=$urlid&URL=$URL\" target=\"anyframe\">$URL</a><br>\n");}
fclose($conn); 
} 
fclose ($linkovi);
}
mysql_close();
$linkovi = fopen("links.html","a");
fwrite($linkovi,"</body></html>");
fclose ($linkovi);

?>
</body>
</html>


----------



## ramandeep000 (Jun 14, 2012)

You can do this using curl library...it saves lots if bandwidth.See this Function.

function checklink($url,$timeout=30){
$ch = curl_init();
$url=$link;
$timeout=30;
// set cURL options
$opts = array(CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $link, // your URL here
CURLOPT_NOBODY => true, // do a HEAD request only
CURLOPT_TIMEOUT => $timeout); // set timeout
curl_setopt_array($ch, $opts);
curl_exec($ch); // do it!
$output = curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200; // check if HTTP OK
curl_close($ch); // close handle
return $output;
}

For details on usage see the original post here.
Checking Dead Links


----------



## harrietadamz (Jun 18, 2012)

whatever scripting language you use, it has to be supported by your hosting company. You should be able to find some scripts that can do this, or hire a programmer to write something


----------

