Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nedevl
Last active October 1, 2020 14:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nedevl/f6fb87ab53f3c765c8eb5e09b16817fe to your computer and use it in GitHub Desktop.
Save nedevl/f6fb87ab53f3c765c8eb5e09b16817fe to your computer and use it in GitHub Desktop.
A useful function written in PHP to check whether a website has a backlink to yours and if it is a Google Friendly one.
<?php
/*
* $backlink - the link we are looking for
* $url - the website against we are checking the $backlink
*
*/
function backlinkCheck($backlink, $url) {
$backlinkexists = false;
$backlinknofollow = false;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);
$dom = new DomDocument();
libxml_use_internal_errors(true);
$dom -> loadHTML($content);
$anchors = $dom -> getElementsByTagName('a');
libxml_clear_errors();
foreach ($anchors as $anchor) {
$link = $anchor -> getAttribute("href");
//check if the link exists among links on the $url
if ($link == $backlink) {
$backlinkexists = true;
$anchorhtml = strtolower($anchor -> C14N());
if (strpos($anchorhtml, 'nofollow') !== false) {
$backlinknofollow = true;
}
break;
}
}
if ($backlinkexists && !$backlinknofollow) {
// backlink exists and it doesn't has a rel attribute with nofollow value
} else if ($backlinkexists && $backlinknofollow) {
// backlink exists and it does has a rel attribute with nofollow value
} else {
// backlink doesn't exists
}
}
?>
@nedevl
Copy link
Author

nedevl commented Feb 22, 2017

If you find it useful, use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment