简体   繁体   English

mod_rewrite映像保护

[英]mod_rewrite image protection

So i'm building a website where users can see a preview of an image in a smaller size, and they have to register to see the full resolution. 因此,我正在建立一个网站,用户可以在其中查看较小尺寸图像的预览,并且他们必须注册才能查看完整分辨率。

I don't thumbnail the images, i just load the whole image, and scale it. 我不缩略图图像,我只是加载整个图像,并缩放它。 Since the images are low-res, this isn't really a problem. 由于图像是低分辨率的,所以这实际上不是问题。

The problem is that users can just right-click on the image en choose 'open in new tab' and they got it. 问题在于用户只需右键单击图像,然后选择“在新选项卡中打开”即可。 I want to disable that in a way that .jpg and .gif files can be seen in the browser, in an -tag, but not when they try to enter the path in the address bar. 我想禁用该功能,以便可以在浏览器中以-tag看到.jpg和.gif文件,但当它们尝试在地址栏中输入路径时则不能。

I'm on Apache. 我在使用Apache。

Is this possible? 这可能吗? Thanks in advance. 提前致谢。

You would need to have a thumbnailed version and a large version. 您需要有缩略图版本和大版本。 When you down-size it you only downsize the display of it. 缩小尺寸时,只能缩小其显示尺寸。 The full-size image is actually already on their computer. 完整尺寸的图像实际上已经在他们的计算机上。

Something along the lines of: 类似于以下内容:

RewriteEngine On
RewriteCond   %{HTTP_REFERER}  !^$
RewriteCond   %{HTTP_REFERER}  !^http://www.yourdomain.com/.*$ [NC]
RewriteCond   %{HTTP_REFERER}  !^http://yourdomain.com/.*$     [NC]
RewriteRule   ^/PublicFiles/*$ /page-about-direct-links.html

untested 未经测试

Change the approach: 更改方法:

  1. If you are showing small image, never use the original image. 如果显示小图像,请不要使用原始图像。 This will take longer to load and it`sa bad habbit. 这将花费更长的时间来加载,并且是个坏习惯。 Use thumbnail instead, save bandwidth, speed the page. 改用缩略图,节省带宽,加快页面速度。

  2. Move the original pictures otside the web folder. 将原始图片移到Web文件夹的另一侧。 This way they can`t be accessed sirectly via a link. 这样,就无法通过链接直接访问它们。

  3. Make a php script that will load the original image if the user is registered. 制作一个php脚本,如果注册了用户,它将加载原始图像。

mod_rewrite can`t check if the user is registered or logged. mod_rewrite无法检查用户是否已注册或登录。 As for the Apache " basic authentican " can be used (but not the best way to do the thing). 至于Apache,可以使用“ basic authentican ”(但不是执行此操作的最佳方法)。

function getImageFromOutside ( $homePath, $file )
{
    if ( file_exists ( $homePath . '/' . $file ) )
    {
        $contents = file_get_contents ( $homePath . '/' . $file );
        return $contents;
    }
}
$homePath = 'some_path/img';

if ( isset ( $_GET['uid'] ) )
{
    $img = $_GET['uid'] . '.jpeg';
    header ( 'Content-type: image/jpeg' );
    echo getImageFromOutside( $homePath, $img );
} else {
    print_r ( $_GET );
    echo file_exists ( $homePath . '/' . $file );
}

Tested, with folder structure 经过测试,具有文件夹结构

  • root
    • img img
    • publichtml 公共html

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

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