title: "Two-way proxied" download with PHP? (one server serves file, and pings another server with client data)
body: <p>Sorry about the "two-way proxied download" terminology - am not sure how this would be otherwise called (correct terminology is much appreciated). Anyways: </p>
  
  <p>Let's assume I have a PDF file of an article, <code>test.pdf</code> (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. </p>
  
  <p>So, for the sake of the example, let's say: </p>
  
  <ul>
  <li>a local PC has a (globally resolvable) IP address 80.80.80.80</li>
  <li>my website server is at <code>myserver.com</code>, with IP address 90.90.90.90
  <ul>
  <li>The link to the PDF there is <code>http://myserver.com/dl/test.php?file=./test.pdf</code></li>
  </ul></li>
  <li>the conference website is at <code>conference.org</code>, IP address 100.100.100.100
  <ul>
  <li>The link to the PDF there is <code>http://conference.org/2001/downloads/test.pdf</code></li>
  </ul></li>
  </ul>
  
  <p>What I want to do is this: when a local PC the PDF file from my website (via <code>http://myserver.com/dl/test.php?file=./test.pdf</code>), the <code>test.php</code> script should also:</p>
  
  <ul>
  <li>initiate a download of <code>http://conference.org/2001/downloads/test.pdf</code>, with the <em>original</em> header data of the client (that is, <code>conference.org</code> should see in their logs that it is 80.80.80.80 which requests), with my website as referrer (that is, 90.90.90.90 would be referrer); the idea of this is that the <code>conference.org</code> webhost would log the same clients that download as <code>myserver.com</code> does </li>
  <li>The download from <code>conference.org</code> should be terminated after 100 bytes or so, so as not to waste the bandwidth of <code>conference.org</code> -- otherwise, it is <code>myserver.com</code> that serves the PDF file</li>
  <li>Should the download from <code>conference.org</code> fail (e.g. if <code>conference.org</code> 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 <code>myserver.com</code>. </li>
  </ul>
  
  <p>Here is an example of <code>test.php</code>, which only does the serving "from <code>myserver.com</code>"; otherwise, the relationship between files local to <code>myserver.com</code>, and their location on <code>conference.org</code>, is simulated in the <code>$filesRelations</code> array:</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;?php
  
  $filesRelations = array(
    './test.pdf'   =&gt; 'http://conference.org/2001/downloads/test.pdf',
  );
  
  if(!(isset($_GET['file']))) {
    echo "&lt;html&gt;
    &lt;head/&gt;
    &lt;body&gt;
    &lt;a href='?file=./test.pdf'&gt;test.pdf&lt;/a&gt;
    &lt;br/&gt; &lt;sub&gt;(".$filesRelations['./test.pdf'].")&lt;/sub&gt;
    &lt;/body&gt;
    &lt;/html&gt;
    ";
  } 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;
    }
  }
  
  exit;
  ?&gt;
  </code></pre>
  
  <p>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 <code>$filesRelations</code> entry, <em>using the header data of the calling client</em>, before it serves the headers and the file (by echoing <code>$includeFile</code>)?</p>
  
  <hr>
  
  <p>For testing, this is the <code>test.tex</code> file (which you can compile with <code>pdflatex test.tex</code> to obtain a <code>test.pdf</code>):</p>
  
  <pre class="lang-latex prettyprint-override"><code>\documentclass{article}
  \usepackage{lipsum}
  
  \begin{document}
  \title{Lorem Ipsum}
  \author{Author's Name}
  \maketitle
  
  \begin{abstract}
  \lipsum[1]
  \end{abstract}
  
  \section{Introduction}
  
  \lipsum[1-12]
  \end{document}
  </code></pre>
  
  <p>(To test, put <code>test.php</code> and <code>test.pdf</code> in one directory, run <code>php-5.4.10 -S localhost:8000</code> in that directory, then visit <code>http://localhost:8000/test.php</code> in the web browser).</p>