2014-05-30 22:28

使用PHP下载“双向代理”? (一个服务器提供文件,并使用客户端数据ping另一个服务器)

  • http-headers
  • php
  • proxy

Sorry about the "two-way proxied download" terminology - am not sure how this would be otherwise called (correct terminology is much appreciated). Anyways:

Let's assume I have a PDF file of an article, test.pdf (see below for Latex example) of which I am an author, which is hosted on a conference website, and otherwise available there for free. Let's say I want to (and am allowed) to also distribute a copy of the same PDF from my website.

So, for the sake of the example, let's say:

  • a local PC has a (globally resolvable) IP address
  • my website server is at, with IP address
    • The link to the PDF there is
  • the conference website is at, IP address
    • The link to the PDF there is

What I want to do is this: when a local PC the PDF file from my website (via, the test.php script should also:

  • initiate a download of, with the original header data of the client (that is, should see in their logs that it is which requests), with my website as referrer (that is, would be referrer); the idea of this is that the webhost would log the same clients that download as does
  • The download from should be terminated after 100 bytes or so, so as not to waste the bandwidth of -- otherwise, it is that serves the PDF file
  • Should the download from fail (e.g. if is temporarily offline), than that should be logged in a text file - but it should NOT otherwise interfere (as in, introduce additional delays) in the process of serving the file from

Here is an example of test.php, which only does the serving "from"; otherwise, the relationship between files local to, and their location on, is simulated in the $filesRelations array:


$filesRelations = array(
  './test.pdf'   => '',

if(!(isset($_GET['file']))) {
  echo "<html>
  <a href='?file=./test.pdf'>test.pdf</a>
  <br/> <sub>(".$filesRelations['./test.pdf'].")</sub>
} else {
  # echo "-- " . $_GET['file'] . " -- "; # dbg
  $localpath = $_GET['file'];
  $fdname = basename($localpath);
  $fsize = filesize($localpath);
  $includeFile = file_get_contents($localpath);

  if ($includeFile === false)
    echo "Error with $localpath";
  } else {
    header("Content-type: application/pdf");
    header("Content-Disposition: attachment; filename=\"".$fdname."\"");
    header("Content-length: $fsize");
    header("Cache-control: private");
    echo $includeFile;


How could I modify this code, so that the script "pings" the link (by initiating and terminating a short, 100-byte download) in the respective $filesRelations entry, using the header data of the calling client, before it serves the headers and the file (by echoing $includeFile)?

For testing, this is the test.tex file (which you can compile with pdflatex test.tex to obtain a test.pdf):


\title{Lorem Ipsum}
\author{Author's Name}




(To test, put test.php and test.pdf in one directory, run php-5.4.10 -S localhost:8000 in that directory, then visit http://localhost:8000/test.php in the web browser).

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dru5089 dru5089 7年前

    I believe the stumbling block is the original header data of the client. The IP address of the client is not contained in a header, but determined by the TCP/IP layer of the socket that the client connected to. That's what would be in the logs. So, it can't be done quite so simply.

    The client's IP can be retrieved by your server via:

    $clientIP = $_SERVER['REMOTE_ADDR'];

    If you were able to come up with a mechanism to let the other server know that this was a request on behalf of a client, you could certainly send them this data. As you point out, perhaps try setting the HTTP_X_FORWARDED_FOR header in your request to that server.

    Best of luck!

    点赞 评论 复制链接分享