簡體   English   中英

PHP:如何隱藏URL

[英]PHP: How to hide URL

我有以下download.php腳本下載文件,效果很好:

<?php

header("Content-Type: application/octet-stream");

$file = $_GET["file"];
header("Content-Disposition: attachment; filename=" . urlencode($file));   
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");            
header("Content-Length: " . filesize($file));
flush(); 
$fp = fopen($file, "r");
while (!feof($fp))
{
    echo fread($fp, 65536);
    flush(); 
} 
fclose($fp);

?>

我想要實現的是隱藏此文件所在的URL,以便當用戶單擊<a target="_blank" href="http://domain.com/files/download.php?file=filename.pdf">Download file</a> ,打開一個沒有URL的新選項卡,並開始下載文件。 實際發生的是新選項卡打開並開始文件下載,但URL欄顯示http://domain.com/files/download.php?file=filename.pdf

如果用php無法做到這一點,我該怎么做呢? 我看過幾次沒有顯示URL的下載,所以我知道這有點可能。

編輯:這就是我想要這樣做的原因:我們將發送一個帶有文件下載鏈接的html郵件,托管此文件下載的網站不是發送郵件的公司的網站。

一如既往,非常感謝你。

這樣做的一種典型方法是將您要提供的文件放在docroot之外。 您的下載腳本應該知道這個地方,並且必須處理所請求的文件名。

例如:

path/in/your/system/docroot/download.phppath/in/your/system/files/filename.pdf

如果有人請求download.php?file=filename.pdf您的腳本必須在path/in/your/system/files/查找此文件,並且必須處理它。

最后,這是不可能的,因為用戶可以打開Goog​​le Inspector並查看“網絡”標簽。

編輯:如果是這種情況,您將使用JavaScript:

編輯2:如果彈出窗口被瀏覽器或插件阻止,請使用后備:

編輯3:如果JS被禁用:

<head><meta http-equiv="refresh" content="5;http://domain.com/files/download.php?file=filename.pdf"> 
    <script>
     var win = window.open("http://domain.com/files/download.php?file=filename.pdf");
      if (!win) {
               window.location = "http://domain.com/files/download.php?file=filename.pdf";      
      }
    </script>
  </head>

編輯4:如果您不希望用戶完全看到用戶站點的域,請讓您的PHP腳本下載該文件,然后將其輸出給用戶:

header("Content-Type: application/octet-stream");

$file = $_GET["file"];
$file = 'http://otherdomain.com/location/' . $file; // This line should suffice for all that you're trying to do
header("Content-Disposition: attachment; filename=" . urlencode($file));   
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");            
header("Content-Length: " . filesize($file));
flush(); 
$fp = fopen($file, "r");
while (!feof($fp))
{
    echo fread($fp, 65536);
    flush(); 
} 
fclose($fp);

您可以使用iframe內容為該網址創建一個新窗口。

var win = window.open("", "Title", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=780, height=200, top="+(screen.height-400)+", left="+(screen.width-840));
win.document.body.innerHTML = "<iframe src='YOUR URL'></iframe>";

如果刪除target="_blank"則不會打開新選項卡,只會下載文件。

將表單作為POST提交並使用$ _REQUEST獲取文件:

$file = $_REQUEST["file"];

暫無
暫無

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

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