1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Is there any way to detect when an image file is requested from my site?

Discussion in 'General Programming Chat' started by tacopalypse, Sep 28, 2010.

  1. tacopalypse

    tacopalypse Executive VIP Jr. VIP Premium Member

    Joined:
    Nov 30, 2009
    Messages:
    980
    Likes Received:
    2,485
    Home Page:
    ok i can't seem to be able to formulate this into a google query, so i'll ask it here:

    i have an image file hosted on my site, which i'm displaying on another site that's not controlled by me.

    what i'd like to do is be able to see how many times the image is viewed on that other site.

    what i've been able to do so far is download the access logs for my site, and in there i can see every single time the image file is requested and the IP that requested it, however downloading & parsing access logs is very inconvenient.

    so, i was wondering if there was any way to do this through a web interface? possibly with php somehow?

    all i would need to do is check how many times a certain file on my site has been requested in a given day.
     
  2. Crazy

    Crazy Jr. Executive VIP

    Joined:
    Jun 13, 2009
    Messages:
    640
    Likes Received:
    319
    Occupation:
    VB, C#, XHTML, CSS, PHP, MySQL, JavaScript, jQuery
    Location:
    Everywhere
    Well, the first question is are you working after the fact? By that I mean, can you modify the source URL on that site that's displaying the image? If so, yes... you can do this with PHP very easily. If you can't modify the image URL on the site, you should try handling it via htaccess. I'm not really good with htaccess coding/configuration, but what you can do very easily would be to force requests to that image file to hit a PHP file. From there, save the data you need (count, accessing IP, etc), then dump the image source from within that script. PM me if you need help.

    htaccess configuration:

    Code:
    redirect 301 /dir/image.jpg http://www.yourdomain.com/track.php
     
    • Thanks Thanks x 1
  3. tacopalypse

    tacopalypse Executive VIP Jr. VIP Premium Member

    Joined:
    Nov 30, 2009
    Messages:
    980
    Likes Received:
    2,485
    Home Page:
    sweet!

    i had no idea it was possible to make a php file deploy an image file like that lol

    here's the code if anyone else is interested:

    PHP:

    <?php

    $imagepath
    ="top.JPG";

    $image=imagecreatefromjpeg($imagepath);

    header('Content-Type: image/jpeg');

    imagejpeg($image);

    ?>

    awesome thanks
     
    Last edited: Sep 28, 2010