.htaccess htaccess -不允许直接访问除已登录用户之外的所有文件(PHP)

0ejtzxu1  于 2022-11-16  发布在  PHP
关注(0)|答案(1)|浏览(142)

使用.htacess(deny from all)-是否可以只允许登录我的系统的用户直接访问文件?如果有什么区别的话,我的网站是用Drupal(PHP)构建的。如果可以的话,那么理想的情况是我也想检查用户的角色。

b4lqfgs4

b4lqfgs41#

您无法单独使用.htaccess来执行此操作。您需要执行以下操作:
1.拒绝所有人访问文件
1.有一个“文件提供程序”脚本,允许在身份验证后通过文件。
示例:

代理服务器.php

<?php 
$proxiedDirectory = "./files/"; //Whatever the directory you blocked access to is.
$filename = isset($_GET["fn"])?$_GET["fn"]:null;

if (!user_is_authenticated()) { //Not a real method, use your own check
    http_response_code(403);
    exit;
}

if ($filename === null || !file_exists($proxiedDirectory.$filename)) {
    http_response_code(404);
    exit;
}


$fp = fopen($proxiedDirectory.$filename, 'rb');

header("Content-Type: image/???"); //May need to determine mime type somehow
header("Content-Length: " . filesize($proxiedDirectory.$filename));

fpassthru($fp);
exit;

您可以通过以下方式使用此功能:
http://example.com/proxy.php?fn=filename.txt

相关问题