【发布时间】:2017-06-11 19:57:29
【问题描述】:
我的 php 脚本创建了一个用户目录来存储私有文件,我需要将其保密,除了所有者。
基本上我的结构是这样的:
- root/
- storage/
---- users/
----------user1/
---------------.htaccess
---------------images/
---------------tmp/
-------------------session-id-file
----------user2/
---------------.htaccess
---------------images/
---------------tmp/
-------------------session-id-file
这意味着当新用户注册时,会在 /storage/users 中创建一个新用户的目录:
$dir = __DIR__ . "/storage/user/" . $user["id"];
mkdir($dir);
然后在里面新建一个.htaccess:
$myfile = fopen($file, "w") or die("Unable to create file!");
$txt = "RewriteCond %{HTTP_COOKIE} PHPSESSID=(\w+)\n
**RewriteCond {CURRENT-DIRECTORY}/tmp/access-%1 -f \n**
RewriteRule ^(.+)$ $1 [L]\n
RewriteRule .+ /deny [L]\n";
fwrite($myfile, $txt);
fclose($myfile);
.htaccess 显示为:
RewriteCond %{HTTP_COOKIE} PHPSESSID=(\w+)
RewriteCond {CURRENT-DIRECTORY}/tmp/access-%1 -f
RewriteRule ^(.+)$ $1 [L]
RewriteRule .+ /deny [L]
当用户登录时,会在用户的 tmp 目录中创建一个新的 session-id-file:
touch($dir . "/tmp/" . session_id());
我的问题是:如何检测 htaccess 所在的{当前目录}?基本上如何修复.htaccess中的错误行:
RewriteCond {CURRENT-DIRECTORYY}}/tmp/access-%1 -f \n
我见过类似的问题,但我无法根据动态创建的目录检测当前目录。
一个不错的选择是在 /users 目录中只使用一个 .htaccess,而不是像我在示例中那样为每个用户使用多个 .htaccess。
感谢您的任何建议:)
【问题讨论】:
-
您要拒绝所有访问/存储/用户吗?我理解对了吗?
-
是的。楼主除外。我使用 session_id 文件和 cookie 检测所有者。
标签: php .htaccess mod-rewrite httpcookie