Check Backlinks With PHP

5th June 2008 - 3 minutes read time

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.

  1. function check_back_link($remote_url, $your_link) {
  2. $match_pattern = preg_quote(rtrim($your_link, "/"), "/");
  3. $found = false;
  4. if($handle = @fopen($remote_url, "r")){
  5. while(!feof($handle)){
  6. $part = fread($handle, 1024);
  7. if(preg_match("/<a(.*)href=[\"']".$match_pattern."(\/?)[\"'](.*)>(.*)<\/a>/", $part)){
  8. $found = true;
  9. break;
  10. }
  11. }
  12. fclose($handle);
  13. }
  14. return $found;
  15. }

Here is an example of the function in use.

  1. if (check_back_link('http://www.google.com','http://www.hashbangcode.com')) {
  2. echo 'link found';
  3. } else {
  4. echo 'link NOT found';
  5. };
  6. // 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

Permalink

is there any script available to check the entire web for backlinks?

Ravi (Sun, 07/31/2011 - 09:13)

Permalink

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.

Permalink

Thanks. Very useful script.

boyama oyunlari (Wed, 02/15/2012 - 15:58)

Permalink

It helps me alot. thank you

Jayanti (Tue, 02/21/2012 - 02:01)

Permalink
Thank you,this is very helpful, it solved my problem.

Lampk (Wed, 04/24/2013 - 13:08)

Permalink
Thanks, just what I was looking for! Do you think there is a way to check if the link on a remote server wasn't commented using, using standard comment tags? Like: <a href="...">  … </a>   (Not sure if my little code sample will show correctly.)

Nick (Wed, 03/19/2014 - 16:18)

Permalink
Excellent tip, I'm begenning a homemade backlink checker to monitor my SEO actions, and your script is very usefull, thanks !

seo (Thu, 11/26/2015 - 18:22)

Permalink
bro. i have test. but doesn't work

Malas Ngoding (Sat, 02/06/2016 - 06:25)

Permalink
thanks. i search this tutorial. and i want to make backlink search

feby (Sat, 02/06/2016 - 06:26)

Permalink
How to count all the number of backlinks a website has and then fetch the url list. Can you please explain. I want to make a backlink checker like ranksignals.com

Anurag Kumar (Fri, 05/13/2016 - 14:14)

Permalink
Hi. Can you please describe. We can count links only or we can get the list of those sites where backlinks are build?

SEO TOOLS (Mon, 02/27/2017 - 15:27)

Permalink
Thanks for this nice snippets. How to check Bulk Backlinks using this snippets from visual editor. Ex. Using a form.

MD. MASUM BILLAH (Mon, 09/25/2017 - 12:06)

Permalink
Thank you for sharing. I was looking for a free tool in google but not satisfied with what I found and want to make my own.

Akash (Sun, 12/31/2017 - 02:43)

Permalink
thanks man..

Akash (Mon, 01/08/2018 - 07:49)

Permalink

Thanks For Sharing Such beautiful information with us. i hope you will
share some more information about this post. please keep sharing! 

John Abraham (Tue, 07/31/2018 - 14:27)

Permalink

Great, thanks for sharing.

vivek raj (Fri, 07/19/2019 - 08:32)

Permalink

thx much for the invitation :)

MixPuh (Thu, 11/26/2020 - 15:44)

Add new comment

The content of this field is kept private and will not be shown publicly.