Backlink Checker


Backlink Checker

 Edit these variables and replace with your own (see source code comments for description):
$url - your website URL
$email - your email
$fromEmail - your email
$subject - subject of report
$body - No need to really modify
$backlinkSources - The sources that have your site URL

 <?php
/* Your Website URL. Leave out the http://
 * and only include the base domain
 */
$url 'free-php-script.blogspot.com';
/*
 * Your email, the place you want the report
 * to arrive at
 */
$email   'your@email.com';
/*
 * Email the report is being sent from
 */
$fromEmail 'from@email.com';
/*
 * Subject of the report email when a backlink
 * is not found on a source
 */
$subject "Backlink not found on Source: $url";
/*
 * Body header text
 */ 
$body "Hai, a source that was suppose to contain a backlink to your website no longer does. Please see below.\r\n\r\n";

/*
 * Backlink Sources
 */
$backlinkSources = array ('http://google.com/',
                          
'http://yahoo.com/',
                          
'http://free-php-script.blogspot.com'
                         
);
/*
 * This is a place holder variable which will
 * be used to hold the true/false variables
 * for report generation
 */
$reportArray = array();
/*
 * Bool value used to determine if any false
 * values are found
 */
$falseFound false;
/*
 * Begin checking the backlinks for the target
 * URL
 */
foreach ($backlinkSources as $source) {
    
// Get the HTML source Code
    
$html file_get_contents $source );

    
// Extract all Links
    
$links ExtractHrefLinks($html);
  
    
// Determine if the links contain your site URL
    
if (CheckForTargetUrl($links$url) === false) {
        
$falseFound true;
        
$reportArray[$source] = 0;
    } else {
        
$reportArray[$source] = 1;
    }
}
/*
 * Dump our results for browser output
 */
print_r($reportArray);
/*
 * If a backlink is not found on a source,
 * Generate an email.
 */
if ($falseFound === true) {
    
GenerateReportEmail($email$fromEmail$subject$body$url$reportArray);
}

/**
 * Send the report email when a source is found that does
 * not have a backlink
 *
 * @var email string
 * @var fromEmail string
 * @var subject string
 * @var body string
 * @var url string
 * @var reportArray array
 *
 * @return null
 */
function GenerateReportEmail($email$fromEmail$subject$body$url$reportArray) {
    
// Create an email header
    
$header "From: " $fromEmail "\r\n"//optional headerfields
  
    /*
     * Loop through the results to form the
     * report
     */
    
foreach ($reportArray as $key => $report) {
        if (
$report == false) {
            
$body .= "Backlink to $url not found on: $key\n";
        }
    }
  
    
// Send the email
    
mail($email$subject$body$header);
}
/**
 * Determine if the array of links contain the
 * target URL (your URL)
 *
 * @var links array of source URLs
 * @var target URL of target website (your website)
 * @return bool
 */
function CheckForTargetUrl($links$target) {  
    
/*
     * Loop through all of the links in the array. Use
     * strpos to test if string is found in link text
     */
    
foreach ($links as $link) {
        if (
strpos($link$target) !== false) {
            return 
true;
        }
    }

    
// Return
    
return false;
}
                          
/**
 * Extract all href Links from an HTML
 * document/text
 *
 * @vars html source code of site
 * @return array
 */
function ExtractHrefLinks($html) {
    
//Create a new DOM document
    
$dom = new DOMDocument;
    
$linkUrls = array();
   
    
/*
     * Parse the HTML. The @ is used to suppress any parsing errors
     * that will be thrown if the $html string isn't valid XHTML.
     */
    
@$dom->loadHTML($html);
   
    
/*
     * Get all links. You could also use any other tag name here,
     * like 'img' or 'table', to extract other tags.
     */
    
$links $dom->getElementsByTagName('a');
   
    
//Iterate over the extracted links and display their URLs
    
foreach ($links as $link){
        
//Extract and show the "href" attribute.
        
$linkUrls[] = $link->getAttribute('href');
    }
  
    return 
$linkUrls;
}?>