Html 如何防止从我的网站下载图像和视频文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1294501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to prevent downloading images and video files from my website?
提问by Haim Evgi
How to prevent downloading images and video files from my website? Is it possible? What would be the best way to do this?
如何防止从我的网站下载图像和视频文件?是否可以?什么是最好的方法来做到这一点?
回答by Josh Leitzel
No, it's not possible.
不,这不可能。
If you can see it, you can get it.
如果你能看到它,你就能得到它。
回答by Chris Ballance
Don't post them to your site.
不要将它们发布到您的网站。
Otherwise it is not possible.
否则是不可能的。
回答by Sampson
Images must be downloaded in order to be viewed by the client. Videos are a similar case, in many scenarios. You can setup proxy scripts to serve the files out, but that doesn't really solve the issue of preventing the user from getting their own copy. For a more thorough discussion of this topic, see the question How can I prevent/make it hard to download my flash video?
必须下载图像才能被客户端查看。在许多情况下,视频也是类似的情况。您可以设置代理脚本来提供文件,但这并不能真正解决阻止用户获取他们自己的副本的问题。有关此主题的更深入讨论,请参阅问题如何防止/使其难以下载我的 Flash 视频?
回答by A. L. Flanagan
I'd like to add a more philosophical comment. The whole intent of the internet, particularly the World Wide Web, is to share data. If you don't want people to download a picture/video/document, don't put it on the web. It's really that simple. Too many people think they can impose their own rules on an existing design. Those who want to post content on the web, and control its distribution, are looking to have their cake and eat it too.
我想补充一点哲学性的评论。互联网,尤其是万维网的全部目的是共享数据。如果您不希望人们下载图片/视频/文档,请不要将其放在网络上。真的就是这么简单。太多人认为他们可以将自己的规则强加于现有设计。那些想要在网络上发布内容并控制其分发的人希望得到他们的蛋糕并吃掉它。
回答by Dan Diplo
In short, no. If someone can view an image or video in their browser then they have, by definition, downloaded it. That's how the web works - it is client server based. Whatever you can view in your browser (client) has been transfered to your computer from the remote website (server).
简而言之,没有。如果有人可以在他们的浏览器中查看图像或视频,那么根据定义,他们已经下载了它。这就是网络的工作方式——它是基于客户端服务器的。您可以在浏览器(客户端)中查看的任何内容都已从远程网站(服务器)传输到您的计算机。
回答by Hasan A Yousef
If you are using PHP, the best way is to control it the .htaccess
, you need to put your files, images and videos under consideration in a separate folder/directory, and create a new .htaccess
file in this directory with the below:
如果您使用 PHP,最好的方法是控制它.htaccess
,您需要将您的文件、图像和视频放在单独的文件夹/目录中,并.htaccess
在此目录中创建一个新 文件,如下所示:
RewriteEngine On
RewriteCond %{REQUEST_URI} \.(mp4|mp3|avi)$ [NC]
RewriteCond %{HTTP_REFERER} !^http://sample.com/.*$ [NC]
RewriteRule ^.* - [F,L]
The first line %{REQUEST_URI}
will prevent getting the file through the web browser or through curl
.
The second line %{HTTP_REFERER}
will prevent accessing the image/video using HTML tags <img>
or <video>
from any website except the exception !
you provide instead of http://sample.com/
which usually should be your website itself.
第一行将%{REQUEST_URI}
阻止通过 Web 浏览器或通过curl
. 第二行将%{HTTP_REFERER}
阻止使用 HTML 标签<img>
或<video>
从任何网站访问图像/视频,除了!
您提供的例外,而不是http://sample.com/
通常应该是您的网站本身。
You can also have a look at my question and the accepted answer herefor more tricks on the browser side.
回答by John MacIntyre
In standard HTML, I don't know of anyway.
在标准 HTML 中,我不知道。
You didn't really say, but I'm guessing you are having problems with people deep linking into your content. If that's the case, and you are open to server side code, I believe this might work:
您并没有真正说,但我猜您在人们深入链接到您的内容时遇到了问题。如果是这种情况,并且您对服务器端代码持开放态度,我相信这可能有效:
- Create a page that accepts a numeric id, maps it to a server file path, opens that file, writes the binary directly to the response stream.
- On the page request, generate a bunch of random ids, and map them to the actual media urls, and store that mapping object server side somewhere (in session?) with a limited life.
- Render your pages with your media links pointing to the new media page with the appropriate id as a query string argument.
- Clear the mapping object and generate all new links on every postback.
- 创建一个接受数字 id 的页面,将其映射到服务器文件路径,打开该文件,将二进制文件直接写入响应流。
- 在页面请求中,生成一堆随机 id,并将它们映射到实际的媒体 URL,并将该映射对象服务器端存储在某处(在会话中?)生命有限的地方。
- 使用指向新媒体页面的媒体链接呈现您的页面,并使用适当的 id 作为查询字符串参数。
- 清除映射对象并在每次回发时生成所有新链接。
This :
这个 :
- won't stop people from downloading from within your page
- definitely isn't as lightweight as standard HTML
- and has it's own set of issues.
- 不会阻止人们从您的页面内下载
- 绝对不像标准 HTML 那样轻量级
- 并且有它自己的一系列问题。
But it's a general outline of a workable process which might help you prevent users from deep linking.
但它是一个可行流程的大纲,可以帮助您防止用户进行深层链接。
回答by David Wolever
As many have said, you can't stop someone from downloading content. You just can't.
正如许多人所说,您无法阻止某人下载内容。你就是不能。
But you can make it harder.
但是你可以让它更难。
You can overlay images with a transparent div
, which will prevent people from right clicking on them (or, setting the background of a div
to the image will have the same effect).
您可以使用透明 覆盖图像div
,这将防止人们右键单击它们(或者,将 a 的背景设置div
为图像将具有相同的效果)。
If you're worried about cross-linking (ie, other people linking to your images, you can check the HTTP referrer and redirect requests which come from a domain which isn't yours to "something else".
如果您担心交叉链接(即,其他人链接到您的图像,您可以检查 HTTP 引荐来源网址并将来自不属于您的域的请求重定向到“其他”。
回答by BurninLeo
As the browser needs to transfer the content to display it (text, images, videos), the data is already on the client's computer when the website is displayed. The previous answers give little advice on how to make it harder for non-experienced users to grab the content. Here are some directions:
由于浏览器需要传输内容来显示它(文本、图像、视频),因此在显示网站时数据已经在客户端的计算机上。之前的答案几乎没有提供关于如何让没有经验的用户更难获取内容的建议。以下是一些方向:
- General
- Overlay the respecitive contents with a transparent
<DIV>
or a transparent image (as described in some answers to this question) - Open the website in a frameset, so saving may miss the frame content.
- Open the website via
window.open()
to hide the menu bar. - Disable right-clicks via JavaScript (not recommended due to all the side-effects on usability)
- Load the page's HTML code from another file (which may check for a specific referer or which may be ROT13) via JavaScript, so it's harder to access the source code.
- Tell the browser that all content is
display:none
for the printer (something like@media print { body, div, p { display: none } }
) - Use JavaScript to hide the content before a client makes a screenshot (see Stop User from using “Print Scrn”)
- Try to disable or overwrite the clipboard (see this post)
- Overlay the respecitive contents with a transparent
- Images
- Do not use the
<img>
tag for images but set the image as background for a<DIV>
- Wrap images into SVGs or Flash movies to make them very hard to access in a usable format.
- Disable caching for images (via
<meta>
tag or by setting the appropriate header on server delivery), so they are not stored in the browser cache (immeaditely accessible on the client's computer). - Cut an image into parts, so it takes some extra work to reconstruct the whole image
- Add
onmousedown
events to images, e.g., display a copyright alert. - Deliver the image via server script (e.g., PHP) and check the referer.
- Do not use the
- Videos
- Stream videos to prevent simple downloading via URL.
- Wrap videos into a Flash movie.
- Use some nasty format that supports DRM.
- Texts
- Make text unselectable (see How to make HTML Text unselectable)
- Additionally to overlaying, wrap the text into JavaScript (e.g., after ROT13 or loaded dynamically from a second file), so the text is not directly available in the source code.
- Convert texts to images (this may decrease display quality), SVGs or Flash
- 一般的
- 用透明
<DIV>
或透明图像覆盖各自的内容(如该问题的一些答案中所述) - 在框架集中打开网站,因此保存可能会错过框架内容。
- 打开网站通过
window.open()
隐藏菜单栏。 - 通过 JavaScript 禁用右键单击(不推荐,因为会对可用性产生所有副作用)
- 通过 JavaScript 从另一个文件(可能会检查特定引用或可能是 ROT13)加载页面的 HTML 代码,因此更难访问源代码。
- 告诉浏览器所有内容都
display:none
用于打印机(类似于@media print { body, div, p { display: none } }
) - 在客户端截屏之前使用 JavaScript 隐藏内容(请参阅阻止用户使用“Print Scrn”)
- 尝试禁用或覆盖剪贴板(请参阅此帖子)
- 用透明
- 图片
- 不要将
<img>
标签用于图像,而是将图像设置为背景<DIV>
- 将图像包装成 SVG 或 Flash 电影,使它们很难以可用格式访问。
- 禁用图像缓存(通过
<meta>
标记或通过在服务器交付时设置适当的标头),因此它们不会存储在浏览器缓存中(在客户端计算机上可立即访问)。 - 将图像切割成多个部分,因此重建整个图像需要一些额外的工作
onmousedown
向图像添加事件,例如,显示版权警报。- 通过服务器脚本(例如,PHP)传送图像并检查引用者。
- 不要将
- 视频
- 流式传输视频以防止通过 URL 进行简单下载。
- 将视频包装成 Flash 电影。
- 使用一些支持 DRM 的讨厌的格式。
- 文本
- 使文本不可选择(请参阅如何使 HTML 文本不可选择)
- 除了覆盖之外,将文本包装到 JavaScript 中(例如,在 ROT13 之后或从第二个文件动态加载),因此文本在源代码中不直接可用。
- 将文本转换为图像(这可能会降低显示质量)、SVG 或 Flash
Again, I repeat that none of this will stop an experienced user from grabbing the content (e.g. by making a screenshop and - optionally - run OCR on it). Sometimes it's as easy as using the browser's developer tools or using the website without JavaScript. Yet, it will give inexperiences users a hard time, so they may look for some easier source to grab from.
再次重申,这一切都不会阻止有经验的用户获取内容(例如,通过制作屏幕截图并 - 可选地 - 在其上运行 OCR)。有时它就像使用浏览器的开发人员工具或使用没有 JavaScript 的网站一样简单。然而,这会给缺乏经验的用户带来困难,因此他们可能会寻找一些更容易获取的资源。
Also keep in mind that the above techniques will affect search engines when reading the page's content (if you're interested in blocking them, start with a robots.txt
).
还要记住,上述技术在阅读页面内容时会影响搜索引擎(如果您有兴趣阻止它们,请从 开始robots.txt
)。
Thank you for any other ideas to complement the above list!
感谢您提供任何其他想法来补充上述列表!
回答by Jordan
It also doesn't hurt to watermark your images with Photoshop or even in Lightroom 3 now. Make sure the watermark is clear and in a conspicuous place on your image. That way if it's downloaded, at least you get the advertising!
现在使用 Photoshop 甚至 Lightroom 3 为图像添加水印也没有什么坏处。确保水印清晰且位于图像的显眼位置。这样,如果它被下载,至少你会得到广告!