Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

How can I check whether someone is using the resources hosted on my website?

I have a website (coded in PHP) and I have uploaded a few images there. I want to know who is using those images on other sites. Normally, the people use image URL to access and display images from my website.

I don't need the code, just need some guidelines. If you can give me some pseudo code, that's appreciated too :P

EDIT:
Seeing the responses, I feel that my question was't well-versed, so to rephrase:

I need the PHP code to check who is using images hosted on my site

Is this possible in PHP? If yes, what should be the algorithm for it? Any coding guidelines?

share|improve this question

migrated from stackoverflow.com Mar 10 '12 at 23:28

This question came from our site for professional and enthusiast programmers.

2  
This is more a question for serverFault as you want to dive into your apache logs and see where the requests are comming from. In general, it not related to programming but server administration. –  Sgoettschkes Mar 10 '12 at 22:31
    
The simplest thing is to check your server access log: find requests for your images and see which refer's IP addresses are used. If your referer is not local - you've got a leecher. –  Aleks G Mar 10 '12 at 22:32
    
Check the HTTP Referer (sic!) request header. If you're lucky, it has a value that might tell you from which site the image was requested. If you like to prevent hotlinking, google for that term and .htaccess. –  hakre Mar 10 '12 at 22:32
1  
@Boo I think webmaster.stackexchange is a better place for this. –  Aleks G Mar 10 '12 at 22:33
    
for all people guiding me to server logs, please check my updated question –  N e w B e e Mar 10 '12 at 22:36

3 Answers 3

up vote 6 down vote accepted

There is no way to find out any information about requests made directly to images hosted on your site using PHP. Since the image served up statically, no PHP script is executed.

You could keep all your images outside your webroot, and load them all through a PHP script that:

  1. Checks the query string to figure out which image to serve
  2. Makes sure the image exists
  3. Outputs a suitable content-type header
  4. Outputs a suitable set of cache control headers
  5. Reads the image data and outputs it

It would also have to deal with If-Modified-Since requests and so on. (So this would be a lot of effort to get right).

Once that was done, you could log any of the information in $_SERVER, including $_SERVER['HTTP_REFERER'] which would tell you the URL of a website linking to or embedding your image (providing the user of the browser did nothing to conceal the referrer header).

You are really much better off just getting the referer information from your webserver's logs.

share|improve this answer
    
really thanks for all of this info..that was i looking for..going to a log and getting the info was not something needs to be asked.. –  N e w B e e Mar 10 '12 at 23:08
    
thanks for this guideline.. –  N e w B e e Mar 10 '12 at 23:08
    
Yes you can protect your site using PHP and monitor images see my answer –  Anagio Mar 11 '12 at 11:12

You can log the referer:

$_SERVER['HTTP_REFERER'];

But please note that this is added by the client and can be overridden. But it should give you some indication to how your site is used.

You can also use something like google analytics to track the users.

share|improve this answer

This guide goes over protecting your sites resources using PHP

http://safalra.com/programming/php/prevent-hotlinking/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.