简体   繁体   English

PHP:如何隐藏URL

[英]PHP: How to hide URL

I have the following download.php script to download a file, which works great: 我有以下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);

?>

What I want to achieve is to hide the URL where this file is located, so that when the user clicks a link such as <a target="_blank" href="http://domain.com/files/download.php?file=filename.pdf">Download file</a> , a new tab opens up with no URL and starts downloding the file. 我想要实现的是隐藏此文件所在的URL,以便当用户单击<a target="_blank" href="http://domain.com/files/download.php?file=filename.pdf">Download file</a> ,打开一个没有URL的新选项卡,并开始下载文件。 What is actually happening is the new tab opens and the file download starts but the URL bar is displaying http://domain.com/files/download.php?file=filename.pdf . 实际发生的是新选项卡打开并开始文件下载,但URL栏显示http://domain.com/files/download.php?file=filename.pdf

If this cannot be done with php, how can I achieve this? 如果用php无法做到这一点,我该怎么做呢? I have seen several downloads where the URL is not shown, so I know this is somehow possible. 我看过几次没有显示URL的下载,所以我知道这有点可能。

EDIT: Here is the reason I want to do this: We will send a html mailing with a link to the file download, and the website where this file download is hosted is not the website from the company which sends the mail. 编辑:这就是我想要这样做的原因:我们将发送一个带有文件下载链接的html邮件,托管此文件下载的网站不是发送邮件的公司的网站。

As always, thank you very much. 一如既往,非常感谢你。

A typical way doing this, is to place the files you want to provide for download outside your docroot. 这样做的一种典型方法是将您要提供的文件放在docroot之外。 Your download script should know about this place and has to process the requested filename considering this. 您的下载脚本应该知道这个地方,并且必须处理所请求的文件名。

For example: 例如:

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

If someone is requesting download.php?file=filename.pdf your script has to look up in path/in/your/system/files/ for this file and has to handle it. 如果有人请求download.php?file=filename.pdf您的脚本必须在path/in/your/system/files/查找此文件,并且必须处理它。

In the end, it's not possible because the user can open Google Inspector and check the Network tab. 最后,这是不可能的,因为用户可以打开Goog​​le Inspector并查看“网络”标签。

Edit: If that's the case, you would use JavaScript: 编辑:如果是这种情况,您将使用JavaScript:

Edit 2: In case the popup is blocked by the browser or addon, use a fallback: 编辑2:如果弹出窗口被浏览器或插件阻止,请使用后备:

Edit 3: In case JS is disabled: 编辑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>

Edit 4: If you don't want the user to see the domain of the user site at all, get your PHP script to download the file and then output that to the user: 编辑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);

You can create a new window with iframe content to that URL. 您可以使用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