2013-04-21 50 views
1

我想在一個任意的二進制文件上創建一個非常簡單的php保護器,用戶輸入密碼並將文件下載到他們的計算機上,但是他們每次要下載時都必須輸入密碼。如何密碼保護二進制文件下載?

first answer的問題Easy way to password-protect php page,行include("secure.html");似乎需要該文件必須是可顯示的ASCII,由瀏覽器渲染。

有沒有一種方法可以保護二進制文件,比如foo.bin具有相同級別的簡單性(以及類似的有限安全級別)?

+0

我推薦在PHP中使用PHPass進行密碼散列,這很容易,也很安全。 http://www.openwall.com/phpass/ – Jonast92 2013-04-21 23:52:48

回答

1

將文件夾設置爲存儲文件的位置以拒絕所有文件,然後使用readfile應該可以訪問它。

<?php 
    if (!empty($_POST)) { 
     $user = $_POST['user']; 
     $pass = $_POST['pass']; 

     if($user == "admin" 
     && $pass == "admin") 
     { 
      $file = 'path/to/file'; 

      if (file_exists($file)) { 
       header('Content-Description: File Transfer'); 
       header('Content-Type: application/octet-stream'); 
       header('Content-Disposition: attachment; filename='.basename($file)); 
       header('Content-Transfer-Encoding: binary'); 
       header('Expires: 0'); 
       header('Cache-Control: must-revalidate'); 
       header('Pragma: public'); 
       header('Content-Length: ' . filesize($file)); 
       ob_clean(); 
       flush(); 
       readfile($file); 
       exit; 
      } 
     } 
    } 
?> 

<form method="POST" action=""> 
    User <input type="TEXT" name="user"></input> 
    Pass <input type="TEXT" name="pass"></input> 
    <input type="submit" name="submit"></input> 
</form> 
+0

我想過使用Apache與HTTP Auth,但現代瀏覽器往往緩存憑據,並且我想強制用戶在每次登錄時輸入密碼。 – merlin2011 2013-04-21 23:53:57

+0

Couldn您是否使用如下頭部:header(「Cache-Control:no-store,no-cache,must-revalidate) – 2013-04-22 00:02:20

+0

不清楚是否所有主流瀏覽器都會服從該服務器指令。 cgi-bin,這可能會進一步限制這種頭文件的使用 – merlin2011 2013-04-22 00:07:48