Why Check for Broken Links? Because Search Engines Don't Like It!
May 15, 2008
Your blog comments may have links to sites that don't exist anymore (broken links for short). You should avoid broken links in your site, simply because search engines don't like it. But you may want to keep the comment, so all you need to do is just remove the link. Use this function to make a check.
<?php /** * check if link exists or broken * * @param string $url the target url * * @return boolean true if link exists, false otherwise */ function is_link_exist($url) { $host = parse_url($url, PHP_URL_HOST); $path = parse_url($url, PHP_URL_PATH); if (empty($path)) { $path = "/"; } /* build request headers */ $reqh = "HEAD $path HTTP/1.1\r\n" . "Host: $host\r\n" . "Connection: Close\r\n\r\n"; $fp = @fsockopen($host, 80, $errno, $errmsg, 30); if (!$fp) { print "Oops, something went wrong."; exit; } /* send request headers */ fwrite($fp, $reqh); /* read response */ while(!feof($fp)) { $resh .= fgets($fp, 4096); } fclose($fp); /* get http status */ $firstline = substr($resh, 0, strpos($resh, "\r\n")); list($proto, $stat, $msg) = explode(" ", $firstline); if ($stat == 200) { return true; } else { return false; } } ?>
Use can use the function above every time you want to add a new comment, or run a script to check your database in a scheduled time. Maybe something like this:
<?php /* * check comments for broken links */ $result = $db->query("SELECT url FROM comments ORDER BY date DESC"); while($row = $result->fetchRow()) { if (!is_link_exist($row->url)) { /* * ...do something with this broken link... */ print "$row->url is broken.\n"; } else { print "$row->url is just fine.\n"; } } ?>
Keywords: broken links, link checking, blog comments, spam links
Share:
Save to del.icio.us
Digg this!

Add your comment