I was wondering, I have made a php plugin for wordpress which asks as an image store. If you login you see the files which you have been assigned. Then you click download and it downloads them.
The trouble is, if you know the URL you can download the image regardless. How can I easily stop users from downloading the image if they know its unique URL, and instead only allow those people who have login permission to view it.
The ideas I have are either:
- Some kind of HTACCESS hack which checks the refering URL
- A kind of KEY system, which only allows the file to be downloaded if a key is supplied.
Any information or ideas would be really useful, thank you.
If you make the images unreadable for the world (directory outside the www dir for instance, or just block it with a .htaccess (no need for referral check) you can be sure that nobody can access your image.
Then serve your image through a file (e.g.
getImage.php?imageid=xxxx
) that checks credentials, and if OK then reads the image (your php process can read the image, e.g. withfile_get_contents()
) and then serve it up.(use the correct header and you'll be fine:
header("content-type: image/your_image_type");
)To secure a file you can do one of following two things
Option 1 is mostly used when people store documents signatures etc Option 2 can be used if you dont have access to such folders like in shared hosting environment etc.
Be sure to replace "mydomain.com" with your own. The above code creates a failed request when hot linking of the specified file types occurs. In the case of images, a broken image is shown instead.