0

I am not asking about regular "execute php on my domain and get URL" but this:

I display image from php, here: http://myservice.com/ShowImage.php?Id=10

So Bobby who runs a website BobbyBlog.net could use this image on his blog like:

<img src=http://myservice.com/ShowImage.php?Id=10>

I want to know that Bobby has loaded image on his website, so what I need to intercept in my ShowImage.php script would be "BobbyBlog.net" string. I am afraid this is not possible with PHP as image request is separated from Bobby blog. What do you think?

Maxitrol
  • 43
  • 4

1 Answers1

0

As far as I know there is not way to capture which site executed your script, like you ask for. The server variable $_SERVER['HTTP_REFERER'] sets the referring page (the page that referred to yours) but I don't think that will have any values in this case. If I am not mistaken, this is the case for most programming languages, as this depends on the web server level, not the programming language. This means that you can not simply use Python or Perl and expect it to work.

Edit: On web server level this should be possible. Linking to images from other sites is commonly referred to as hotlinking. There are several guides on how you can disable this. Here is one for Apache.

Edit2: Looking deeper at the problem it may seem like you can inspect the $_SERVER['HTTP_REFERER'] value. This is described in this related SO question. It also mentions that this is not possible if sent over HTTPS, and that the value can be spoofed. In those cases there is no way of knowing the origin. Hope it helps.

Community
  • 1
  • 1
OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
  • There must be something on server level or how could some sites prevent loading of images by referring to them in img tag? -- handle this in .htaccess maybe? – Maxitrol Feb 13 '17 at 18:13
  • @Maxitrol Yes. This is called hotlinking. I've included a tutorial I found in my original answer. I hope that it helps. If you feel that my answer helped you, please accept my answer. – OptimusCrime Feb 13 '17 at 20:09
  • Your solution is interesting but it just blocks hotlinking. What I want to do (see my original question) is that I want to know who is trying to display image from my website. So if you hotlink from "Optimuscrime.com/blog.html" I want to grab that URL and save it... somehow I should change that .htaccess directive to include URL of the website where hotlink has been made. – Maxitrol Feb 14 '17 at 19:55
  • @Maxitrol I've updated my answer yet again, see Edit2. That is as much as I know and further Googling around does no reveal other ways of doing this. The `{HTTP_REFERER}` variable in Apache is the same as the value in `$_SERVER['HTTP_REFERER']`. Hope it helps. – OptimusCrime Feb 14 '17 at 21:11