我在.htaccess文件中有一个重写规则:
RewriteRule ^folder/(.*)$ folder/handle.php?path=$1 [L]
使用handle.php
文件对用户进行身份验证,并查看他们是否拥有高级帐户。
我想[1]检查用户是否未通过身份验证,然后页面显示错误,otherwise
[2]下载开始&我不想使用任何PHP类或脚本来处理文件下载(只是正常的服务器端下载,没有PHP处理)。
我怎样才能做到这一点?有可能吗?
请求文件下载的URL:http://mywebsite.com/folder/file.zip
您拥有的重写规则很好。。。除非您可能应该添加一个条件来检查并确保REQUEST不是"handle.php",否则您可能会得到一个重定向循环。
现在,在handle.php文件中,这将处理该文件夹中的所有文件请求。
在handle.php中,可以使用$_GET['path']
来获取请求的文件名。在handle.php中,可以包含身份验证检查。如果身份验证检查通过,则可以继续对用户执行readfile
。handle.php示例:
<?php
set_time_limit(0);
session_start();
include "../some_functions_auth_file.php";
// NOTE: better file checking should be implemented here. We're using basename() for now.
$file = !empty($_GET['path']) ? basename($_GET['path']) : false;
if($file === false || !file_exists($file)) die("Invalid file.");
if(user_is_authenticated()) {
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false );
header("Pragma: no-cache" );
header("Content-Type: application/octet-stream");
header("Content-Length: " .(string)(filesize($file)) );
header('Content-Disposition: attachment; filename="'.$file.'"');
header("Content-Transfer-Encoding: binary'n");
readfile($file);
exit;
} else {
header("Location: ../login.php");
}
?>
请注意,这是非常基本且未经测试的
现在,如果您不想使用readfile
(因为它很慢),那么也许您可以设置一个Apache环境变量。。。然后,在.htaccess中,您可以检查该变量是否存在,如果存在,则允许下载。否则,将用户重定向到登录名。