防止直接在浏览器中打开图像 [英] Prevent opening image in browser directly
问题描述
我有一个带有图片的文件夹1.jpg,2.jpg,3.jpg ...有没有办法阻止用户直接在浏览器中输入www.example.com/pictures/3.jpg等URL加载该图像?
I have a folder with pictures 1.jpg, 2.jpg, 3.jpg... Is there any way to prevent user to type the URL like www.example.com/pictures/3.jpg directly into browser and loading that image?
如果web html调用它(< img href = ...),则应加载图像。
The image should be loaded if web html calls it (< img href=...).
这可能吗?使用IIS URL重写或其他一些技术?
Is that possible to do? Using IIS URL Rewrite or some other technique?
我使用的是IIS7.5。我的目标是阻止用户看到下一张图片......我知道,我可以编码名称,但是我有一些旧的数据库,从1-1000开始,我想以某种方式阻止用户不使用网址浏览没有引用者...因为每天我都在服务一张照片而且我不希望他们找到其他照片......
I am using IIS7.5. My goal is to prevent users to see the next image... I know, I could have names encoded, but I have some old database that goes from 1-1000 and I'd somehow like to prevent just users not to browse using url with no refferer... Because every day I am serving one picture and I don't want that they find the rest...
这有可能吗?
推荐答案
您可以依靠 HTTP_REFERRER
尝试使用网址重写但是这并不总是准确的,并且可能会阻止某些浏览器上的用户查看您网站上的图片。
You can try it with a url rewrite by relying on HTTP_REFERRER
but that's not always accurate and could possibly block users on some browsers from seeing the images on your site as well.
如果我是你,我会将你的所有图片移到你的网站之外web目录(或者最好只是阻止图片
文件夹)然后构建一个像这样的php脚本image.php:
If I were you I would move all of your images outside your web directory (or preferably just block the pictures
folder altogether) and then build a php script like this called image.php:
<?php
define('NUM_IMAGES', 1000);
header('Content-Type: image/png');
$image = imagecreatefromjpeg('pictures/'.((int)(time()/86400)%NUM_IMAGES+1).'.jpg');
imagepng($image);
?>
上面的脚本会将图像输出到用户浏览器,每天更换一次到下一张图片按顺序,您可以使用它:< img src =image.php/>
The script above will output an image to the users browser which will change once a day to the next image in sequence and you can use it like: <img src="image.php" />
然后,由于您的图像文件夹被阻止,没有人可以看到任何其他图像。然后仍然可以直接请求image.php,但他们只会看到当天的图像。
Then, since your images folder is blocked, nobody can see any other image. Then can still request image.php directly but they'll only see the image of the day.
如果您不想每天自动轮换一次并想要手动控制它显示的图像你也可以只用'。((int)(time()/ 86400)%1000 + 1)。'
替换<想要显示的图像。
If you don't want to rotate automatically once a day and want manual control over which image it shows you can also just replace the '.((int)(time()/86400)%1000+1).'
with the number of the image you want to display .
如果你想让它自动旋转,但想要控制它更新的时间你可以为 time()
喜欢:((time()+ $ offset)/ 86400)
If you do want it to rotate automatically, but want to control the time it updates at you can add an offset to time()
like: ((time()+$offset)/86400)
这篇关于防止直接在浏览器中打开图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!