Tech Support banner

Not open for further replies.
1 - 3 of 3 Posts

1 Posts
Discussion Starter #1
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:

#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", "");
$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 = 986;
//$URL = "";
$URLInfo = parse_url($URL);

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


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


1 Posts
You can do this using curl saves lots if bandwidth.See this Function.

function checklink($url,$timeout=30){
$ch = curl_init();
// 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
1 - 3 of 3 Posts
Not open for further replies.