【问题标题】:How to give the AuthUserFile access only for specific page?如何仅授予特定页面的 AuthUserFile 访问权限?
【发布时间】:2020-01-07 23:46:57
【问题描述】:

我的根文件夹中有一个页面,名为export.php。现在我不想将这个页面的访问权限授予所有用户。

我想要实现的目标是,如果任何用户尝试访问export.php 页面,则会显示一个警报并询问用户名和密码。登录详细信息正确后,即可访问该页面。我在 htaccess 上尝试了一些代码。

现在我有两个问题,

1) 我在所有页面上都收到警报。如何只为export.php页面设置?

2) 输入用户名和密码后,出现服务器错误。

htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]


AuthType Basic
AuthName "Access to the Hidden Files"
AuthUserFile 'http://localhost:8080/example/.htpassword'
Require valid-user

解决方案

首先我找到了使用路径 <?php echo $_SERVER['DOCUMENT_ROOT']; ?>

输出:/opt/lampp/htdocs/example/

然后我在 htaccess 文件中添加了路径

SetEnvIfNoCase Request_URI /export SECURED

AuthName "Access to the Hidden Files"
AuthType Basic
AuthUserFile "/opt/lampp/htdocs/example/.htpasswd"
AuthGroupFile /
Require valid-user

Satisfy    any
Order      Allow,Deny
Allow from all
Deny from env=SECURED


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

【问题讨论】:

  • AuthUserFile 应该是完整的文件系统路径而不是 URL
  • 抱歉,我没看懂。我必须使用系统路径?
  • 应该是AuthUserFile /full/path/to/.htpassword.htpassword 不应该有组或其他人的读/写权限。
  • 我使用的是 MAC,所以我必须使用类似这样的东西 nfs://192.168.64.2/opt/lampp/htdocs/example/.htpassword ?
  • @anubhava,是的,我认为路径是正确的,我尝试使用双引号并且它正在使用特定页面。你能帮我解决第二个问题吗?输入正确的详细信息后,它会重定向到服务器错误。

标签: php .htaccess .htpasswd


【解决方案1】:

全部重写到router.php,然后从php重写(小mvc例如https://github.com/breakermind/Tronix)。

更好的解决方案,检查用户是否在php文件中有权限:

<?php
// User field from users table (add to session when logging)
if($_SESSION['user']['allow_export'] == 9 && isIpAddressAllowedFunc($_SERVER['REMOTE_ADDR'])){
    // show content
}else{
    // Redirect or log out user
    header('Location: index.php');
    exit;
}

?>

【讨论】:

  • 这是我的第二个问题的答案吗?
  • 这个例子展示了如何解决这个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-24
  • 2013-10-15
  • 2014-02-18
  • 2021-11-28
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多