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;
}?>

AD text

  AD text
<?
if (!isset($title)){ $title = "<b>Sponsored Links:</b>";} // set to "" to exclude title.
if (!isset($adsfile)){ $adsfile = "ads1.txt";} // file containing one hyperlink per line
if (!isset($maxads)) { $maxads = 4; } //max ads to show
if (!isset($spacing)){ $spacing = 3; } //spacing count between ads
if (!isset($spacer)) { $spacer = "&nbsp;"; }  //set to <br> for a vertical list, to &nbsp; for a space
if (!isset($titlespacing)){ $titlespacing = 2; } //spacing count between title and ads


//==============================SCRIPT STARTS HERE
if (file_exists($adsfile))
{
    $ads = array_filter(file($adsfile)); // get the list from the file
   
    if (count($ads) > 0 && $maxads > 0) //only go on if the ads file isn't empty, and maxads has is > 0
    {
        if (count($ads) > $maxads) // there are plent of ads to choose from, so get a random selection
        {
           
            if ($title != "")
            {
                echo "$title" . str_repeat($spacer,$titlespacing);
            }
           
            $selectedkeys = array_rand($ads,$maxads); //get a mixed random list of keys from the ads   
           
            if ($maxads > 1)
            {           
                //print each link in the shuffled order
                foreach ($selectedkeys as $key)
                {
                    if ($ads[$key] != "")
                    {
                        print "$ads[$key]" . str_repeat($spacer,$spacing);
                    }
                }
            }
            else // maxads is "1" just get the one link
            {
                print "$ads[$selectedkeys]" . str_repeat($spacer,$spacing);
            }
       
        }
        else //if maxads is greater than ads, just print whatever is in ads
        {
            if ($title != "")
            {
                echo "$title" . str_repeat($spacer,$titlespacing);
            }
           
            if ($maxads > 1)
            {
                foreach ($ads as $ad)
                {
                    if ($ad != "")
                    {
                        print "$ad" . str_repeat($spacer,$spacing);
                    }
                }
            }
            else
            {
                print "$ads[0]" . str_repeat($spacer,$spacing);
            }
        }//end if count(ads) > maxads
   
    }//end if count and max ads > 0
   
}
else
{
    //nothing to do here.  replace with banner or alternate ad code if you want!
}


?>

Get the Current Page URL

 Get the Current Page URL 

<? $url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; ?>

Scale an image using PHP


Scale an image using PHP


<form action="/upload.php" enctype="multipart/form-data" method="post">
<input id="upload" name="upload" type="file" />
<input id="final-upload" type="submit" value="Upload Image" /></form>

<?php
define( 'MAX_FILESIZE', 20000 );
define( 'UPLOAD_PATH', $_SERVER['DOCUMENT_ROOT'] . 'uploads/' );
    
if( !empty( $_FILES['upload'] ) && $_POST )
{

//some error code checking
if( !empty( $_FILES['upload']['error'] ) )
{
switch( $_FILES['upload']['error'] )
{
 case '1':
 $error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
 break;
case '2':
     $error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
  break;
case '3':
  $error = 'The uploaded file was only partially uploaded';
  break;
  case '4':
     $error = 'No file was uploaded.';
       break;
 case '6':
      $error = 'Missing a temporary folder';
      break;
 case '7':
        $error = 'Failed to write file to disk';
            break;
    case '8':
        $error = 'File upload stopped by extension';
       break;
      case '999':
        default:
            $error = 'No error code avaiable';
        }
    }
elseif( empty( $_FILES['upload']['tmp_name'] ) )
{
    $error = 'No file was uploaded.';
    }
    // Is the file an image?
    elseif( !preg_match( '|image/|', $_FILES['upload']['type'] ) )
    {
        $error = 'File is not an image.';
    }
    // Is the file small enough?
    elseif( $_FILES['upload']['size'] > MAX_FILESIZE )
    {
        $error  = 'File is too big.';
    }


if( !$error )
    {
        // Find the uploaded file extension
        $ext    = substr( $_FILES['upload']['name'], strrpos( $_FILES['upload']['name'], '.' ) + 1 );
   
        // If the main file has uploaded, proceed to create the thumbnail
    if( move_uploaded_file( $_FILES['upload']['tmp_name'], UPLOAD_PATH . $_FILES['upload']['name'] ) )
        {
            $main_image = UPLOAD_PATH . $_FILES['upload']['name'];
   
            // Create the thumbnail
            $make_thumb = create_thumb( $main_image, 100, 100, UPLOAD_PATH . 'thumb_' . $_FILES['upload']['name'], $ext );
   
            // Check that the thumbnail was created
            if( !$make_thumb )
            {
                $error = 'Thumbnail was unable to be created.';
            }
        }
        else
        {
            $error  = 'Failed to upload image. Ensure directories are created properly.';
        }
    }
function create_thumb( $imgSrc, $thumbnail_width, $thumbnail_height, $dest_src, $ext )
    {
        //getting the image dimensions
        list( $width_orig, $height_orig ) = getimagesize( $imgSrc );

        // Check if the images is a gif
        if( $ext == 'gif' )
        {
            $myImage = imagecreatefromgif($imgSrc);
        }
        // Check if the image is a png
        elseif( $ext == 'png' )
        {
            $myImage = imagecreatefrompng($imgSrc);
        }
        // Otherwise, file is jpeg
        else
        {
            $myImage = imagecreatefromjpeg($imgSrc);
        }
   
        // Find the original ratio
        $ratio_orig = $width_orig / $height_orig;
   
        // Check whether to scale initially by height or by width
        if( $thumbnail_width / $thumbnail_height > $ratio_orig )
        {
            $new_height = $thumbnail_width/$ratio_orig;
            $new_width  = $thumbnail_width;
        }
        else
        {
            $new_width      = $thumbnail_height*$ratio_orig;
            $new_height = $thumbnail_height;
        }
   
        $x_mid = $new_width / 2;  //horizontal middle
        $y_mid = $new_height / 2; //vertical middle
   
        $process = imagecreatetruecolor( round( $new_width ), round( $new_height ) );
   
        // Scale the image down and the reduce the other axis to create the thumbnail
        imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
        $thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
        imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($thumbnail_width/2)), ($y_mid-($thumbnail_height/2)),   $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height);
   
        // Depending on the file extension, save the file
        if( $ext == 'gif' )
        {
            imagegif( $thumb, $dest_src );
        }
        elseif( $ext == 'png' )
        {
            imagepng( $thumb, $dest_src );
        }
        else
        {
            imagejpeg( $thumb, $dest_src, 100 );
        }
   
        // Remove rubbish file data
        imagedestroy($process);
        imagedestroy($myImage);
   
        // Return thumb ( success / fail )
        return $thumb;
    }
}?>

Valid URL

Valid URL

$url = http://www.tutorialcadet.com

if (!preg_match('/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&amp;?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/', $url)) {
 
echo 'The URL you entered was invalid. Be sure to include <strong>http://</strong>';
 
}

 

Integer contain between 1 and 16 characters

 Integer contain between 1 and 16 characters
$int = 4;
 
if (!preg_match('#^[0-9]{1,16}$#', $int)) {
 
echo 'That is not a valid integer.';
 
}

string contain between 4 and 28 characters

string contain between 4 and 28 characters


$string = "tutorial";
 
if (!preg_match('#^(.){4,128}$#', $string)) {
 
echo 'The string you entered is invalid.';
 
}

E-mail Validation

E-mail Validation

$email = 'editor@thedaily.com'
 
if (!preg_match('^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$^', $email)) {
 
echo 'The email address you entered was invalid.';
 
}