I would like to create a web site with many images. But I would like to protect against direct access to images, e.g. direct links to images without visiting the web site.
What is the preferred way to do this? And what are the alternatives with Pros and cons?
I have some ideas (I don't know if they are possible):
- File permissions
- PHP Sessions
- Temporary file names or URLs
- HTTP Redirection?
Maybe this isn't practiced on many web sites? E.g. I tried to access a private photo on Facebook without beeing logged in, but I could still visit the photo.
The platform will probably be a Ubuntu machine with NginX and PHP.
I use both methods - checking user gent and and referrer. User agent I check at .htaccess. And referrer check at php file. You can see it at http://coloring-4kids.com
Here is my code:
watermark.php
You could use a PHP script to retrieve the images using something like:
<img src="mysite.com/getimage.php?id=001" />
and have the PHP script return the image data only after confirming that the domain of the HTTP_REFERER is your's.
If you have an account-oriented site, I suggest using PHP sessions as you stated and have the PHP script verify the session before returning the image data.
This is going to be hard to do. In order for your clients' web browsers to access the pictures, they need to be readable. File permissions won't work because you'll need to grant access to the browser. You won't be able to stop someone from downloading them and doing something with them.
If you only want to stop direct linking, if you change the filenames on a regular basis and update your pages to reflect this, other pages will have their links broken.
Add a simple
.htaccess
file in your site folder with the follwoing linesNote I added also
js
andcss
file even if I think it's bizzare to find someone who attempts to scrape them.This might be useful: Allow/deny image hotlinking with .htaccess
Edit: One thing to note about this method is that some Browser/AV/Firewall software removes Referer data when you browse, which would cause potentially legitimate users to be treated as hotlinkers.
If your site already uses some kind of authentication or session system, then it would be better to use the method given in @Mark Baijens' answer.
Update: NGiNX rewrite rule to prevent hotlinking:
You can dynamically protect a folder using htaccess and the users ip.
Add a .htaccess file to your images folder with the following lines:
Then use PHP to insert the users ip into the htaccess file when they log in like this:
The folder will be blocked from any ip that is not logged in.
Notice that I checked to see if the ip is valid. It is important that you give the user no way to inject their own code into your htaccess file.
Also notice that I put the users id in a comment to the right of the ip in the htaccess file. When the user logs out you can search the htaccess file and remove the ip of the user.
You can update this on every request to prevent users who are using dynamic ips from getting kicked off.
I use this method with my entire members areas, it is an excellent added layer of security. Just make sure that you put your log in scripts outside of the members folder.