简体   繁体   English

防止使用浏览器URL直接访问图像

[英]Prevent direct access to images using the browser url

I have a folder named - Images . 我有一个名为-Images的文件夹。 This folder contains user profile pictures. 此文件夹包含用户个人资料图片。 Right now a user can see his image by just copying the image URL to his browser any time. 现在,用户可以随时通过将图像URL复制到浏览器来查看其图像。 This way he can also see other user's profile pics. 这样,他还可以查看其他用户的个人资料照片。 What I want to achieve is - The user should be able to see his profile pic only through the PHP page on my website. 我想要实现的是-用户应该只能通过我网站上的PHP页面看到其个人资料图片。 If the user directly puts the image URL, it should not be displayed. 如果用户直接放置图像URL,则不应显示它。

I tried to achieve this using .htaccess. 我尝试使用.htaccess实现此目的。 This is what I have in the .htaccess file : 这是我在.htaccess文件中的内容:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mysite.com/
RewriteRule \.(gif|jpg)$ http://www.mysite.com/errorpost.jpg [R,L]

I am new to .htaccess. 我是.htaccess的新手。 If there is a way to achieve this, please help. 如果有办法实现这一目标,请帮忙。

Thanks in advance. 提前致谢。

I have the same problem. 我也有同样的问题。 Currently I found 2 ways: 目前,我发现了两种方法:

1) base64_encode() + ajax + js/jquery 1)base64_encode()+ ajax + js / jquery

  • Store every image encoded by base64_encode() in binary file or database OUTSIDE www folder. 将base64_encode()编码的每个图像存储在二进制文件或数据库OUTSIDE www文件夹中。
  • Use ajax to get that data. 使用ajax获取该数据。 it should return "data:image/jpeg;base64,$enc_imgbinary" 它应该返回“ data:image / jpeg; base64,$ enc_imgbinary”
  • Replace 'src' of 'img' attribute with returned result using js/jquery 使用js / jquery将'img'属性的'src'替换为返回结果

pros 优点

  • impossible to access image with direct link 无法通过直接链接访问图像

cons 缺点

  • I did found not similar solution for video. 我没有找到类似的视频解决方案。
  • images should be encoded beforehand (or first usage) to minimize server CPU usage. 图像应事先编码(或首次使用),以最大程度地减少服务器CPU使用率。
  • encoded images takes about 30% more space on disk => 1.3x disk space 编码的图像占用磁盘大约30%以上的空间=> 1.3倍磁盘空间
  • if you want to keep original images on server => 2.3x disk space. 如果要将原始图像保留在服务器上=> 2.3x磁盘空间。
  • ~30% more data will be send over the network 将通过网络发送约30%的数据

2) long-random-names (+symlinks) 2)长随机名称(+符号链接)

A) Store images in www folder using long-random names A)使用随机名称将图像存储在www文件夹中
B) Store images outside www folder with symlinks to www folder. B)将图像存储在带有www文件夹符号链接的www文件夹之外。 (images outside www can also work as your desktop images backup) (www之外的图像也可以用作您的桌面图像备份)

notes: 笔记:

  • folders must also contain random symbols 文件夹还必须包含随机符号
  • use '.' 采用 '。' before any folder or file name => just in case, to prevent from displaying folder content on unconfigured apache 在任何文件夹或文件名之前=>以防万一,以防止在未配置的apache上显示文件夹内容
  • configure apache to folow symlinks in case B) (add FlowSymlinks to httpd.conf) 配置apache以在情况B中遵循符号链接)(将FlowSymlinks添加到httpd.conf)
  • configure apache to prevent folder content listing (remove Indexes from httpd.conf) 配置apache以防止列出文件夹内容(从httpd.conf删除索引)
  • example of image hierarchy: 图像层次结构示例:

    • www 万维网
      • .media_jmdue7jed .media_jmdue7jed
        • .user1_hash_!sdfsewewfsdfsds .user1_hash_!sdfsewewfsdfsds
          • .album1_name_!jfie8e7y77667fef .album1_name_!jfie8e7y77667fef
            • .photo1_name_!kjio9i890v8fsd978fyreshf .photo1_name_!kjio9i890v8fsd978fyreshf
            • .photo2_name_!09098dfuujdsif87s7ysdffd .photo2_name_!09098dfuujdsif87s7ysdffd
            • ... ...
          • .album2_name_!ghhyuflp!huidfjh .album2_name_!ghhyuflp!huidfjh
            • .photo1_name_!feojihudhufuuhfrufhi8484 .photo1_name_!feojihudhufuuhfrufhi8484
            • .photo2_name_!2344gfdgfdgdfefedw232sdg .photo2_name_!2344gfdgfdgdfefedw232sdg
            • ... ...
        • .user1_hash_!j333re89dsfdsf .user1_hash_!j333re89dsfdsf
          • ... ...

pros: 优点:

  • Can be used for video also 也可以用于视频
  • You can still keep original images with original names outside www folder by creating symlinks to www folder using long-random-names. 通过使用长随机名称创建指向www文件夹的符号链接,您仍然可以将具有原始名称的原始图像保留在www文件夹之外。 Even different for every user. 每个用户甚至都不同。
  • Image can be used outside your server (in forums, quick send direct link to your friend or similar) 图片可以在服务器外部使用(在论坛中,可以将直接链接直接发送给您的朋友或类似人)

cons 缺点

  • symlinks must be created beforehand (or on-fly) 必须预先创建符号链接(或动态创建)
  • Image can be accessed with direct link 可以通过直接链接访问图像
    • however its nearly imposible to guess it 但是几乎不可能猜到它
    • also you can change symlink random-name periodicaly or on image rights change (I quess google+ does so) 您也可以定期更改符号链接随机名称或更改图像版权(我要求google +这样做)
  • map original-name → long-random-name should be stored in db (or sidecard/meta file) 映射原始名称→长随机名称应存储在db(或边卡/元文件)中
    • (can be bypassed if you keep original-name inside long-random-name by encoding/decoding or by combining original-name + long-random-name) (如果通过编码/解码或通过组合原始名称+长随机名称来将原始名称保留在长随机名称内,则可以绕过该路径)

===================================== ====================================

I had implemented case 1) and it worked for me fine, however I did not found similar solution for HTML5 video. 我已经实现了案例1),对我来说很好用,但是我没有找到类似的HTML5视频解决方案。

Case 2) seems more flexible. 情况2)似乎更灵活。 However I still not sure about security. 但是我仍然不确定安全性。 If anyone sees security holes please let me know. 如果有人看到安全漏洞,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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