簡體   English   中英

僅當用戶登錄時才允許顯示圖像。

[英]Allowing images to display only when user is logged in.

我是PHP / Web開發的新手,正在嘗試創建安全的文件查看器。 我正在使用一種表單來登錄PHP會話,該表單會檢查phpmyadmin數據庫中的有效信息。

我的問題是如何禁用直接路徑訪問,但允許用戶登錄后查看圖像?

我已經嘗試過.htaccess文件。 我已經禁用了該路徑,但是無法弄清楚如何顯示該路徑。

一旦可以顯示圖像,是否有人對我如何確保只有登錄的人可以查看它們有任何建議?

非常感謝!

PS:第一篇文章! :)(似乎在其他線程上找不到足夠的信息)

您應該添加一個PHP腳本,該腳本檢查用戶是否已登錄,如果輸出,則輸出圖像(文件的類型)。 您可以為此使用readfile ,該頁面上有詳細的示例。

if ($userIsLoggedIn) {
    $path  = realpath($imagePath . '/' . $_GET['filename']);
    if (substr($path, 0, strlen($imagePath) + 1) == $imagePath) {
        // Check that the user doesn't request ../../../etc/shadow or something
        // Add headers
        header('Content-Type: image/jpeg');
        readfile($imagePath . '/' . $_GET['filename']);
    }
}

您可以將文件置於普通訪問權限之外,並通過PHP如下訪問它們:

header('Content-type: '.$type);  //$type = 'image/jpeg' but can be other file type
header('Content-Length: ' . filesize($path . $filename));
header('Content-Disposition: attachment; filename=MyFileName.ext');
readfile($path . $filename);
exit();

檢查用戶是否可以查看此文件,然后如上所示顯示它。 如果要顯示圖像,可以放棄Content-Disposition。

但是請確保在此之前您不發送其他數據。

http://www.boutell.com/newfaq/creating/forcedownload.html

暫無
暫無

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

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