Backlinks are an important part of search engine optimisation and are also useful in seeing what sort of things are popular on your site. If you have a list of known backlinks that you want to keep track of then you can do it manually, or you can get a script to do it for you.
The following function takes two arguments, the first being the remote URL and the second is the URL you are checking for. The function works by doing a small amount of initial formatting on the URL you are checking for and then downloading the remote page in little bits and seeing if each bit contains a link. If it does then the function breaks out and returns true. If the link isn't there then it returns false.
function check_back_link($remote_url, $your_link) {
$match_pattern = preg_quote(rtrim($your_link, "/"), "/");
$found = false;
if($handle = @fopen($remote_url, "r")){
while(!feof($handle)){
$part = fread($handle, 1024);
if(preg_match("/<a(.*)href=[\"']".$match_pattern."(\/?)[\"'](.*)>(.*)<\/a>/", $part)){
$found = true;
break;
}
}
fclose($handle);
}
return $found;
}
Here is an example of the function in use.
if (check_back_link('http://www.google.com','http://www.hashbangcode.com')) {
echo 'link found';
} else {
echo 'link NOT found';
};
// this prints 'link NOT found', unfortunately...
To get the most out of this function it is best to have a plain text file full of all of the links that you think you have (one per line) and use the file() function in PHP to load all of the URLs into an array. you can then loop through this array and see which sites have a link to your site and which don't.
Also, if you are looking at a lot of URLs then you might want to include a call to set_time_limit() function with a parameter of something like 300 at the top of the script. This stops the script from timing out after 30 seconds (default) and your script will probably take a little longer than this to run.
Comments
is there any script available to check the entire web for backlinks?
You would need to spider the entire web. I would sugest using something like Google Webmaster Tools or even SEOmoz pro as they keep track of back links.
Thanks. Very useful script.
It helps me alot. thank you
…
(Not sure if my little code sample will show correctly.)Thanks For Sharing Such beautiful information with us. i hope you will
share some more information about this post. please keep sharing!
Great, thanks for sharing.
thx much for the invitation :)
Great article. I love all your articles.
Nice, code it works I have used it.
i'm beginning a homemade backlink checker to monitor my SEO actions, and your script is very useful, thanks !