0

我的根文件夹中有一个页面,名为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]
4

1 回答 1

0

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

更好的解决方案,检查用户是否在 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;
}

?>
于 2019-09-05T08:05:46.963 回答