[英]ActionScript - Loading Assets From Password Protected Directory (.htaccess) Using PHP?
[英]Is it possible to read from protected directory using only PHP/HTML?
我有一个目录,其中包含在特定日期之前不能在世界范围内访问的数据。
自然,该目录不应直接通过网络浏览器读取。 我目前使用.htpasswd和.htaccess解决此问题。
但是,有一个世界可读的.php文件,其目录向上一级。 基于日期的PHP文件有条件地生成从受保护目录读取的基本HTML标记(例如<img .../>
)。
不幸的是,在我的测试中,.php文件需要进行身份验证才能加载数据。 我的问题是我是否正在尝试做根本上不可能的事情,或者是否可以对其进行调整以使其起作用。 另外,如果有可能,我是否应该了解其他任何问题(安全性或其他方面)?
附加信息:
我猜测您可能遇到的问题是,即使您尝试从不受保护的php文件中输出<img src="protected.jpg" />
,也可以显示HTML,但不能显示图像文件本身。
如果我正确理解您要做什么,则需要:
编辑:代理示例:我似乎无法在网上找到示例,因此当我希望控制从PHP对文件的访问时(例如,这可能是敏感数据,其访问权限需要通过$进行验证),这是我经常使用的功能_SESSION或DB值):
function send_binary_data($path, $mimetype, $filename = null){
@ob_clean();
if($filename === null) $filename = basename($path);
$size = filesize($path);
//no-cache
header('Cache-Control: no-cache, must-revalidate, public');
header('Pragma: no-cache');
//binary file
header('Content-Transfer-Encoding: binary');
//mimetype
header('Content-Type: ' . $mimetype);
header('Content-Length: ' . $size);
header('Content-Disposition: inline; filename=' . $filename);
header('Content-Description: ' . $filename);
$chunksize = 1 * (1024 * 1024);
$buffer = '';
$handle = fopen($path, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
print $buffer;
}
$result = fclose($handle);
unset($handle);
$handle = null;
die();
}
当然,您仍然需要限制来自.htaccess的直接访问,但是如果是代理,您会将所有请求重定向到不受保护的代理脚本,如下所示:
RewriteEngine ON
RewriteRule ^(.*)$ /proxy.php?file=$1 [NC,QSA]
和proxy.php将包含类似:
if(!isset($_GET['file'])) die('file not set');
$file = $_GET['file'];
//perform all your custom checking, including security checks due to retrieving data from $_GET, and if access is granted :
$path = 'yourpath/'.$file;
$mimetype = 'defineregardingyour/ownrules';
send_binary_data($path, $mimetype);
.htaccess仅用作直接从Internet到目录的访问控制。
PHP访问由chmod权限控制。 尝试将其更改为755。您仍然可以使用.htaccess-file对其进行密码保护或任何其他类型的保护。
考虑到添加的注释,我假设您正在尝试在输出中包括受保护目录内的图像。 自然,未经身份验证的用户无法访问它们...为什么还要保护它们?
您可以将需要全球访问的文件添加到.htaccess文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.