SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Backlink Check in PHP

    I'm looking for something to check backlinks in php - I've found a few examples but nothing that works so far.

    The one below will only return a positive result, and I'm not sure why -
    Code:
    <?php
    $myUrl = 'http://www.jpg5.com';
    $recipUrl = 'http://www.google.com';
    
    $content=file_get_contents($recipUrl,FALSE,NULL,0,20);
    $pos = strpos($myUrl, $content);
    
    if($pos === false){
    echo "Link There";
    //link is not there
    
    } else {
    echo "Link NOT There";
    //link is there
    
    }
    ?>
    clearly google isn't linking to my site..but the script returns that it is. any idea where I went wrong? Or a better way to do this? Thanks.

  2. #2
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Logic error on your part. look at your script carefully

    PHP Code:
    if($pos === false){
    echo 
    "Link There";
    //link is not there

    } else {
    echo 
    "Link NOT There";
    //link is there


    should be
    PHP Code:
    if($pos === false){
    echo 
    "Link NOT There";
    //link is not there

    } else {
    echo 
    "Link There";
    //link is there


  3. #3
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    LOL! Can't believe I did that.

    How ever I'm getting the same result (it's always negative, even when I use a page that I know has a particular link on it):
    I can use this thread for example (which I know links to the php section):
    Code:
    <?php
    $myUrl = 'http://www.sitepoint.com/forums/forumdisplay.php?f=34';
    $recipUrl = 'http://www.sitepoint.com/forums/showthread.php?t=565307';
    
    $content=file_get_contents($recipUrl,FALSE,NULL,0,9999999);
    $pos = strpos($myUrl, $content);
    
    if($pos === false){
    echo "Link NOT There";
    //link is not there
    
    } else {
    echo "Link There";
    //link is there
    
    }
    ?>
    But I get a negative result again

  4. #4
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Look at your file_get_contents function and the context in which its used.

    According to the manual...
    file_get_contents( string $filename [, int $flags [, resource $context [, int $offset [, int $maxlen ]]]] )

    According to your usage, you are starting with the first byte of the file and only going 20 bytes in.
    (Obviously, you need MUCH more than that.)

    Try removing the everything but the filename from your file_get_contents method.

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just gave that a go with the following:
    Code:
    <?php
    $myUrl = 'http://www.sitepoint.com/forums/forumdisplay.php?f=34';
    $recipUrl = 'http://www.sitepoint.com/forums/showthread.php?t=565307';
    
    $content=file_get_contents($recipUrl);
    $pos = strpos($myUrl, $content);
    
    if($pos === false){
    echo "Link NOT There";
    //link is not there
    
    } else {
    echo "Link There";
    //link is there
    
    }
    ?>
    But it still returns a negative each time Thanks for the help though, I appreciate it.

  6. #6
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echo out an htmlentities version of $content and you will see the link in the source returned is something like this:
    <a href="forumdisplay.php?s=516eda4679db70722fc3a6b11d74A1a2&amp;f=34">PHP</a>

    .. you will need to use regular expressions.
    This may be a little buggy on the regex part (need to prevent f=34 from being found if it comes anywhere after the closing quotation)

    PHP Code:
    <?php
    $look_in_url 
    'http://www.sitepoint.com/forums/showthread.php?t=565307';
    $look_in file_get_contents($look_in_url);

    $look_for 'forumdisplay\.php\?(.*f=34)';
    $found preg_match("/$look_for/i"$look_in);

    if (
    $found){
        echo 
    "Link is There";
    }
    else {
        echo 
    "Link is NOT There";
    }
    ?>

  7. #7
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by simshaun View Post
    echo out an htmlentities version of $content and you will see the link in the source returned is something like this:
    <a href="forumdisplay.php?s=516eda4679db70722fc3a6b11d74A1a2&amp;f=34">PHP</a>

    .. you will need to use regular expressions.
    This may be a little buggy on the regex part (need to prevent f=34 from being found if it comes anywhere after the closing quotation)

    PHP Code:
    <?php
    $look_in_url 
    'http://www.sitepoint.com/forums/showthread.php?t=565307';
    $look_in file_get_contents($look_in_url);

    $look_for 'forumdisplay\.php\?(.*f=34)';
    $found preg_match("/$look_for/i"$look_in);

    if (
    $found){
        echo 
    "Link is There";
    }
    else {
        echo 
    "Link is NOT There";
    }
    ?>

    PERFECT! Thank you so much, that does exactly what I need it to do.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •