簡體   English   中英

如何只為特定頁面授予AuthUserFile訪問權限?

[英]How to give the AuthUserFile access only for specific page?

我的根文件夾上有一頁名為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']; ?>找到了路徑<?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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM