0

So I've got a lot of statistic images.

I post them for free on my website.

I encourage people to use them on their own site or platform.

So it's fine people that are doing this. But I would like to know what sites or IP addresses are hot linking and have some analytics (views eg) on them.

Is this possible? I understand when people download an use tracking is impossible so I only want to track the hot linking part.

I was thinking maybe with .htaccess?

I currently do not have a PHP script in place that serves these images.

You can just grab https://example.com/image.png

Or should I use PHP to serve image script? Or maybe an online service?

Server logs are not an option to use.

tvdsluijs
  • 127
  • 11
  • Does this answer your question? [Allow/deny image hotlinking with .htaccess](https://stackoverflow.com/questions/1245869/allow-deny-image-hotlinking-with-htaccess) – Robo Robok Nov 27 '22 at 08:51
  • 1
    You are looking for a way to analyze the access logs to those resources. There are existing tools for that task, for example perl scripts that can extract that form of information. If that is not enough you will have to change the way people access the resources. Instead of immediately delivering the resource from file by the http server you'd need to use a routing logic which is able to store each access in a database before handing out the resource. A wide spread technique, you will find many examples for such router scripts. You will have to adapt those to your needs, though. – arkascha Nov 27 '22 at 09:52

1 Answers1

0

As @arkascha stated in comments, you should analyze your web server's (Apache) access logs where this information should already be recorded. Specifically, the Referer (one r) HTTP request header. (Although this data is not 100% reliable since user's can suppress the Referer header the browser sends if they so choose and websites themselves can also suppress the referrer by setting a referrer-policy.)

The Referer header contains the URL from which the user is referred. A hotlinked image on another site (pulling an image from your site) will contain the URL on which the image is displayed (barring some restrictions). However, a direct request (if a user types the image URL directly into their browser) does not generate a referrer and the Referer header is empty.

Note that whilst the Referer that is logged contains information about the website that is hotlinking the image. The corresponding IP address that is logged is the IP address of the individual user making the request.

You should not need any additional .htaccess or PHP script for this, unless you have more specific requirements.

MrWhite
  • 43,179
  • 8
  • 60
  • 84