簡體   English   中英

htaccess - 禁止直接訪問除登錄用戶以外的所有文件 (PHP)

[英]htaccess - disallow direct access to all files except logged in users (PHP)

Using.htacess (deny from all) - 是否可以只允許登錄我系統的用戶直接訪問文件? 如果它有任何區別,我的網站是用 Drupal (PHP) 構建的。 如果這是可能的,那么理想情況下,我也想檢查用戶的角色。

您不能單獨使用.htaccess來執行此操作。 你需要做的是:

  1. 拒絕所有人的文件訪問
  2. 有一個“文件提供者”腳本,允許在身份驗證后通過文件。

例子:

代理.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM