开发者

mod_rewrite image protection

开发者 https://www.devze.com 2023-02-18 15:10 出处:网络
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.

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 i开发者_如何学编程mage, 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.

I'm on 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`s a bad habbit. Use thumbnail instead, save bandwidth, speed the page.

  2. Move the original pictures otside the web folder. 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.

mod_rewrite can`t check if the user is registered or logged. As for the Apache "basic authentican" can be used (but not the best way to do the thing).

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
    • publichtml
0

精彩评论

暂无评论...
验证码 换一张
取 消