1

I have a php site am trying to track number of times my downloads get hit using the following code:

                //update download count
                update_attachment_counter($attachment);

                // We'll be outputting a PDF
                header ( 'Content-Type: application/pdf' );
                header ( 'Content-Disposition: inline; filename="fileName.pdf"');
                header('Content-Length: ' . filesize($attachment));
                readfile ( $attachment );
                die ();

I am seeing my counters go up multiple times per download. My attachments are anywhere from 2 to 30MB in size. Does it make since that I am seeing multiple requests per download? How do I only track each download once?

mjr
  • 1,963
  • 4
  • 20
  • 21
  • 1
    possibly the download is having to be restarted by the user due to network failure OR there is an error in update_attachment_counter (the code to which you didn't post) – developerwjk Jun 14 '17 at 22:59
  • update_attachment_counter does a ++ on a database value. Nothing exciting and works great if I die immediately following it. I did not feel like posting my schema, etc :) – mjr Jun 14 '17 at 23:01
  • how about changing update_attachment_counter to log date, ip,file then it would be easer to debug. still easy to do a querry for the count –  Jun 14 '17 at 23:03
  • I know what the counter should be because I'm the only person who can hit it right now, so I don't have to worry about multiple people trying to download things simultaneously yet. Do you think the browser might be restarting the download due to a timeout or something? – mjr Jun 14 '17 at 23:05
  • Are you using any browser extensions that might be doing multi-segment downloads? – jmoerdyk Jun 14 '17 at 23:08
  • HEAD requests before the actual downloads or parallel downloads. – arkascha Jun 14 '17 at 23:09
  • @jmoerdyk I don't think so. I am currently testing with chrome so unless it is doing it automatically? – mjr Jun 14 '17 at 23:11
  • If your connection is HTTP you might be able to tell using fiddler2 if you can spot multiple simultaneous requests – Dave S Jun 14 '17 at 23:32

1 Answers1

1

Due to the large file size, I am getting partial download requests. By checking for HTTP_RANGE I was able to determine if it was the original request or the partial request:

if(!isset($_SERVER['HTTP_RANGE']))
{
  .... update counter
}

See using php to download files, not working on large files?

mjr
  • 1,963
  • 4
  • 20
  • 21