简体   繁体   English

防止图像直接访问

[英]Prevent Images from direct access

Is there any way I can setup a gallery that is accessible just to the members? 我有什么办法可以建立一个只供会员使用的画廊? I can setup the php login and all. 我可以设置php登录名和全部。 But i want the images to be handled securely. 但我希望图像得到安全处理。 Anyone having the image URL should not be able to access the image until not logged in as a member. 具有图像URL的任何人都必须先以成员身份登录,才能访问该图像。

You can use things like: 您可以使用以下内容:

Have the images in a location which is not accessible to the public. 将图像放置在公众无法访问的位置。

Use code for images like: 对以下图像使用代码:

<img src="/imageproxy.php?image=<?=$imagename?>" />

And an imageproxy.php file: 还有一个imageproxy.php文件:

$image = filter_input(INPUT_GET,"image");

$imageWithPath = "/path/to/images/".$image
if (/* is authenticated */ && is_readable($imageWithPath) && /* user can access $image*/) {
    header("Content-Type: ".mime_content_type($imageWithPath));
    $handle = fopen($imageWithPath,"r");
    fpassthru($handle);
    fclose($handle);
    exit; 
} else {
   http_response_code(403);
   exit;  
}

This is an alternative to using base64 images since this way you can load images asynchronously instead of pre-loading them. 这是使用base64映像的替代方法,因为您可以通过这种方式异步加载映像,而不是预加载映像。

To prevent images from direct access you can set sessions and display things if session is set 为了防止图像直接访问,您可以设置会话并在设置了会话后显示内容

<?php
session_start();
if(isset($_SESSION['username']) && !empty($_SESSION['username']))
        {
             echo "<img src='imgname.png' />";
        }
?>

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

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