使用浏览器的URL prevent直接访问图像 [英] Prevent direct access to images using the browser url

查看:140
本文介绍了使用浏览器的URL prevent直接访问图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个命名的文件夹 - 图片。此文件夹包含用户个人资料图片。现在,用户可以通过只图像URL复制到他的浏览器的任何时间看到他的图像。这样,他也可以看到其他用户的个人资料照片。我想实现的是 - 用户应该只能通过我的网站上的PHP页面,查看他的个人资料照片。如果用户直接将图像的URL,它不应该被显示。

I have a folder named - 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. 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. If the user directly puts the image URL, it should not be displayed.

我试图做到这一点使用的.htaccess。这是我在.htaccess文件中:

I tried to achieve this using .htaccess. This is what I have in the .htaccess file :

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

我是新来的.htaccess。如果有一种方法可以做到这一点,请大家帮忙。

I am new to .htaccess. If there is a way to achieve this, please help.

在此先感谢。

推荐答案

我有同样的问题。目前,我发现了2种方式:

I have the same problem. Currently I found 2 ways:

1)base64_en code()+ AJAX + JS / jQuery的

  • 将base64_en code()的二进制文件或数据库外部WWW文件夹中存储的每个图像的连接codeD。
  • 使用Ajax来获得这些数据。它应该返回数据:图像/ JPEG; BASE64,$ enc_imgbinary
  • 替换的'IMG'src'中使用带有返回的结果属性JS / jQuery的

优点

  • 不能访问图像直接链接

缺点

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

2)长期随机名称(+符号连接)

使用长随机名
在WWW文件夹中)将图像存储   WWW文件夹外的B)存储图像与符号链接到www文件夹中。      (WWW以外的图像也可以作为你的桌面映像备份)

A) Store images in www folder using long-random names
B) Store images outside www folder with symlinks to www folder. (images outside www can also work as your desktop images backup)

注:

  • 文件夹必须还包含随机的符号
  • 使用'。任何文件夹或文件名之前=>以防万一,从上未配置Apache的显示文件夹中的内容prevent
  • 配置Apache folow中的情况B符号链接)(添加FlowSymlinks到httpd.conf文件)
  • 配置Apache以prevent文件夹内容列表​​(从httpd.conf中删除索引)
  • 例如图像层次:

  • 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
  • configure apache to folow symlinks in case B) (add FlowSymlinks to httpd.conf)
  • configure apache to prevent folder content listing (remove Indexes from httpd.conf)
  • example of image hierarchy:

  • WWW
    • .media_jmdue7jed
      • .user1_hash_!sdfsewewfsdfsds
        • .album1_name_!jfie8e7y77667fef
          • .photo1_name_!kjio9i890v8fsd978fyreshf
          • .photo2_name_!09098dfuujdsif87s7ysdffd
          • ...
          • www
            • .media_jmdue7jed
              • .user1_hash_!sdfsewewfsdfsds
                • .album1_name_!jfie8e7y77667fef
                  • .photo1_name_!kjio9i890v8fsd978fyreshf
                  • .photo2_name_!09098dfuujdsif87s7ysdffd
                  • ...
                  • .photo1_name_!feojihudhufuuhfrufhi8484
                  • .photo2_name_!2344gfdgfdgdfefedw232sdg
                  • ...
                  • ...

                  优点:

                  • 可用于视频也
                  • 您仍然可以通过创建采用长随机名称的符号连接到www文件夹中保留原始图像与WWW文件夹以外的原始名称。即使是不同的为每一位用户。
                  • 图像可以在服务器外使用(在论坛上,快速直接发送链接给您的朋友或类似)

                  缺点

                  • 符号链接必须事先创建(或飞)
                  • 在图片可直接链接访问
                    • 然而其近IMPOSIBLE猜测它
                    • 你也可以periodicaly或肖像权的变化(我quess谷歌+这样做)改变符号链接随机名称
                    • 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)
                      • (如果你通过编码/解码,或通过结合原来的名称+长随机名内保持原有名称长随机的名字被绕过)
                      • (can be bypassed if you keep original-name inside long-random-name by encoding/decoding or by combining original-name + long-random-name)

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

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

                      我已实施的情况下1)和它的工作对我来说很好,但是我并没有发现类似的解决方案对HTML5视频。

                      I had implemented case 1) and it worked for me fine, however I did not found similar solution for HTML5 video.

                      案例2)似乎更为灵活。不过,我仍不能确定安全性。 如果有人看到了安全漏洞,请让我知道。

                      Case 2) seems more flexible. However I still not sure about security. If anyone sees security holes please let me know.

                      这篇关于使用浏览器的URL prevent直接访问图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆