[英]How can I deny access to all files in directory but allow access from specific php file in other directory?
[英]Deny access to files in root directory from invalid users with php
我有一个PHP网站。 要访问某些页面,用户必须登录。我提供了一个脚本,如果未经身份验证,可以重定向到登录页面。 用户必须寻找专门为他们制作的图像,其他用户则不应看到这些图像。 为了显示这些图像,我使用了img标签,据我所知它们必须位于根目录中。 有什么方法可以保护这些文件免受无效用户的侵害?
与其链接到<img src='1.jpg'/>
类的图像,而不是链接到检查用户已登录并回显图像字节的php <img src='getimage.php?imageId=1'/>
...,然后将其放置在该php页面之外不可直接使用的位置。
//check if user is logged in
if(!loggedIn())
{
echo 'not logged in';
exit();
}
$id = $_GET['imageId'];
$file = ... //use that id to look in db for image path
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.