[英]How to give the AuthUserFile access only for specific page?
我的根文件夹上有一页名为export.php
。 现在,我不想授予所有用户访问此页面的权限。
我要达到的目的,如果有任何用户尝试访问export.php
页面,则会显示一个警报,并询问用户名和密码。 登录信息正确无误后,即可访问该页面。 我在htaccess上尝试了一些代码。
现在我有两个问题,
1)我在所有页面上都收到警报。 如何只为export.php页面设置?
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']; ?>
找到了路径<?php echo $_SERVER['DOCUMENT_ROOT']; ?>
<?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]
将所有内容重写为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;
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.