Grabbing current URL of an page that's being hotlinked (image), with PHP

Discussion in 'PHP' started by TSelbeck, May 22, 2011.

  1. #1
    Hi,

    I am trying to find out the websites that are hot linking my images. I don't have server stats enabled, and my access logs are around 2GB :X

    Let's say ABC.com has a webpage, and in the webpage.. they are hotlinking an image of mine...

    <img src="http://cde.com/image1.jpg">
    Code (markup):
    I figured I'd simply use .htaccess to allow image1.jpg to be a .php file (let's say.. hotlink.php). Then I could grab the page URL, insert it into a database and Bob's your uncle.

    Then, I ran into a problem. I wasn't sure how I'd get PHP to display the URL of the web-page that is hosting the image. HTTP_HOST, HTTP_REFERER, REMOTE_HOST ($_SERVER) do not work - they just return things such as hotlink.php (which is of course, right))

    I did try toying with .htaccess a little to see if it was possible through there, but.. I'm no expert on it

    The site is using around 1GB of bandwidth every 3 minutes.. and is driving me crazy.

    Any advice would be a huuge help.
     
    TSelbeck, May 22, 2011 IP
  2. jazzcho

    jazzcho Peon

    Messages:
    326
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The sites are hotlinking to you BUT the request happen from the user 's browser. I am not sure the browser sends any referrer information in that case. If it does not, there 's not much you can do this way.

    Here 's another trick you can do.

    On your site, add a cookie on every visitor of a page. Now, when a request happens for your image, redirect them to a doorway script that will check for that cookie. If found --> server the image. If not, server empty image.
     
    jazzcho, May 22, 2011 IP
  3. TSelbeck

    TSelbeck Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hey Jazzcho,

    Thanks for the reply. Just as I thought, I wasn't too sure what information would be sent. Come to think of it, would would be just as good.. is a search engine that allows you to search source code

    The second method is interesting. Although, I am wanting to identify the websites which are hotlinking, as opposed to preventing them hotlinking. This way I can identify which websites (hopefully) are causing the issue, and maybe contact them about the issue.

    Thanks
     
    TSelbeck, May 22, 2011 IP
  4. Sefrez

    Sefrez Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you have rewrite_module enabled you could try this:
    .htaccess
    
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} .*jpg$|.*gif$|.*png$ [NC]
    RewriteRule (.*) image.php?img_file=$1&ref=%{HTTP_REFERER}
    
    Code (markup):
    image.php
    
    // code here for tracking etc...
    $img = $_GET['img_file'];
    $ref = $_GET['ref'] == null ? 'Directly Accessed' : $_GET['ref'];
    
    // Change to MySQL database or what ever your needs are
    $h = fopen('data.txt', 'a');
    fwrite($h, 'Image File: '.$img.' Accessed From: '.$ref."\r\n");
    
    // deal with displaying image
    $ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));
    header('Content-type: image/'.$ext);
    
    echo file_get_contents($img);
    
    PHP:
    The only problem that I found is that when the image is accessed directly, the browser (FireFox for me) seems to send two request as image.php is executed twice thus taking two tracking's for "one" time being accessed. I'm not sure if there is an easy way to fix this as it seems to be browser related, however it could be accounted for. You would have to go through the pain though of adding first time access logs and compare them to the second access. If it is within little time (taking into account other variables like access IP, image being accessed, etc,) then you would have it not add the log. This may not be a problem for you though. The script did, however, take the location of the file hotlinking the image. It was all on the same server so you will still need to see if it works over servers (I'm unsure).

    Good luck. I don't know much about htaccess and was able to get together this by searching a bit. So it may not be as good as you can get.
     
    Sefrez, May 22, 2011 IP
  5. TSelbeck

    TSelbeck Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hey Sefrez,

    Thanks for that! Life saver! That works a treat.

    Used MySQL to add the results to the database. If it accessed by my site, or is a direct access, then it is ignored (for now.. but before I disabled them, there were about 3,500 in around 30 seconds.. so might have to look into that, too!) - and is yielding some interesting results

    Again, thanks very much!

    Tom
     
    TSelbeck, May 23, 2011 IP