簡體   English   中英

防止直接訪問html文件,但允許通過PHP?

[英]Prevent direct access to html file but allow through PHP?

我有一個我希望保護的網站部分。 我創建了一個PHP腳本來驗證密碼,一旦驗證,我希望它們被定向到一個HTML頁面。

www.mywebsite.com/protectedfolder/index.html

我不希望用戶使用上述網址,將其粘貼到瀏覽器中,然后訪問該網頁。 我必須添加我的.htaccess文件嗎?

將其添加到父文件夾中的.htaccess文件中:

RewriteEngine On

RewriteBase /
RewriteRule ^protectedfolder/index\.html$ / [NC,R,L]
# OR THIS TO PROTECT ALL FOLDER
# RewriteRule ^protectedfolder / [NC,R,L]

或者在受保護的文件夾中創建.htaccess文件並寫下:

Order Deny,Allow
Deny from all
Allow from 127.0.0.1

當php腳本運行時,它從當前用戶訪問文件(而不是通過http)。 因此,當嘗試使用php的file_get_contents(),readfile(),fopen()等從受保護的文件夾中讀取文件時,apache的規則不會阻止php使用文件系統進行IO操作。

如果我們想要將受保護文件夾中的輸出文件發送給經過身份驗證的用戶,我們必須運行以

if(hasAccess()) {
    print readfile('path/to/protectedfolder/file.html');
}

我會使用PHP和一個帶密碼輸入的表單來提供服務器的POST請求。

<form action="index2.php" method="POST">
    <input name="pass" type="password" />
</form>

然后在PHP中

if (empty($_POST["pass"]) || $_POST["pass"] != "yourpasshere") {
    header("Location index1.html");
    die();
}
//your second page here

編輯:您也可以使用會話進行此操作,用戶在某個位置登錄,您為他們提供一個會話名稱,如$_SESSION["name"] = "admin"; 然后以類似於上面的方式檢查它

session_start();
if (empty($_SESSION["name"])) {
    header("Location index1.html");
    die();
}
//your second page here

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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