Check Dead/Broken Links From Database Using PHP CURL:
Hola Friends ! Checking Deadlinks From the database manually is a Headache ,So why not use a script which return the http status of the particular link and tell us if the link is dead or not.So how do we check the dead links from the database ? How do we programatically check whether the link is dead or not ?
To check broken or dead links from Database we will use curl .Curl is PHP library which can make head HTTP head requests which are good enough to find if a link is broken/dead or not.So why not we use CURL rather than fopen which uses more bandwidth.CURL saves our bandwidth and time.
So I have made a Script using PHP curl which check the deadlinks,which is Quite simple and easy.You can use this for checking single link or Use a for loop to iterate through the link from your database.So lets take a look at script which checks dead links from your database and iterate through each link and send a curl request to check whether link is invalid or not.
PHP Script To Check For Dead Links From Database:
Here is the Php function which check the dead links using curl.you could call it by supplying your url to this function and this function will return true if the link is online and false if link is dead.You can use this function to check the dead link .This can be used to check single link or multiple links.
function checklink($url,$timeout=30){
$ch = curl_init();
$link=$url;
$tout=$timeout;
// set cURL options
$opts = array(CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $link, // your URL here
CURLOPT_NOBODY => true, // do a HEAD request only
CURLOPT_TIMEOUT => $tout); // 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;
}
That was easy ! now let me show you how to use this with multiple link from the database.
Check Invalid/Dead Links From Database :
Now we’ll use the same above function to check dead links from the database .We will iterate through the Links in our database . So we will connect to database collect all the links and iterate through all the links and if the link is dead then we store the link id in the array.that’w how we find all the dead links from the database.
$con=mysql_connect("localhost","username","password");
mysql_select_db("multimedia",$con);
$sql="select id,link from videos";
$result=mysql_query($sql);
$deadLinks=array();
while($row=mysql_fetch_array($result))
{
$is_dead=checklink($row['link']);//call te fucntion and pass it the link
if(!$is_dead){
$deadLinks[]=$row['id'];//array will hold all deadlink id's
}
}
That is how we Check all our dead links from the database using the curl library of the php.Also when this script run on server rather than desktop it will be much more faster.If you have 1000 or more link then it might give timeout error on localhost(xampp,wamp) , so to try it on the localhost use the limit ,then it will work fine.But on server it will work fine whether you have 1000 or 5000 links.
I hope You likes this Script ,Thanks for visiting on Design Aeon and keep visiting.

You should also use CURLOPT_FOLLOWLOCATION option, because of redirections. Your script will report 301 codes as invalid, but url can redirect to another page which is OK and returns 200 code.
Your checklink function features a number of typos… I doubt it ever works the way it is
($timeout is overiden, $url is replaced by undefined variable $link… )
Tnx Dude ,,I didn’t noticed that,,,
Hi
Thanks for your great Social Media Plugin. Is there a way to include a facebook share button.
http://www.islandlinkx.com/blog