[英]htaccess - disallow direct access to all files except logged in users (PHP)
Using.htacess (deny from all) - 是否可以只允許登錄我系統的用戶直接訪問文件? 如果它有任何區別,我的網站是用 Drupal (PHP) 構建的。 如果這是可能的,那么理想情況下,我也想檢查用戶的角色。
您不能單獨使用.htaccess
來執行此操作。 你需要做的是:
例子:
代理.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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.