简体   繁体   中英

How to keep users from viewing images by URL?

Imagine I have an image file secretImage.png on my Webspace and I want several users to have the right to access this image on index.php . This can easily be done:

// index.php
if($userIsAllowed) echo '<img src="secretImage.png" />';

The problem is, users could theoretically still access the image by browsing directly to www.myDomain.com/secretImage.png . Is there any way to prevent them from doing so, so that the image can only be viewed inside index.php ?

You can use htaccess to route all requests for a specific folder through a .php file. In the file, you can determine whether user can see the file or not... if so, deliver the correct headers and the image. If not, show whatever you want (404, placeholder image).

The HTTP call GET /secretImage.png should itself require authentication. For example, you could use basic authenication and have the user send a Authorization: {base64 user:password} header.

Serve your images from a php file via fpassthru where you check that the user is allowed

So for example, you might have a php file, getimage.php, that accepts a querystring like "?image=mysecretimage.png" and if the user is allowed, it passes it through. If you go this route, be careful to take appropriate precautions that only your image files are passed through.

User-specific URLs can be guarded through HTTP authentication. There are examples online, and it is entirely possible through some .htaccess fiddling that you tailor this method to all of your existing images.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM