Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking file complete download

I have a file hosting site and users earn a reward for downloads. So I wanted to know is there a way I can track whether the visitor downloaded whole file so that there are no fake partial downloads just for rewards.

Thank You.

like image 790
Shishant Avatar asked Feb 24 '10 21:02

Shishant


2 Answers

If you could monitor the HTTP response codes returned by your web server and tie them back to the sessions that generated them, you would be in business.

A response code of 206 shows that the system has delivered some of the information but not all of it. When the final chunk of the file goes out, it should not have a response code of 206.

If you can tie this to user sessions by putting the session code inside the URL, then you could give points based on a simple log aggregation.

like image 143
TheJacobTaylor Avatar answered Sep 19 '22 09:09

TheJacobTaylor


I implemented a similar solution on a file hosting website.

What you want to do is use the register_shutdown_function callback that allows you to detect the end of execution of a php script regardless of the outcome.

Then you want to have your file in a non-web accessible location on your server(s), and have your downloads go through php: idea being that you want to be able to track how many bytes have been passed to the client.

Here's a basic way of implementing (eg:)

<?php
register_shutdown_function('shutdown', $args);

$file_name = 'file.ext';
$path_to_file = '/path/to/file';
$stat = @stat($path_to_file);

//Set headers
header('Content-Type: application/octet-stream');
header('Content-Length: '.$stat['size']);
header('Connection: close');
header('Content-disposition: attachment; filename='.$file_name);

//Get pointer to file
$fp = fopen('/path/to/file', 'rb');
fpassthru($fp);

function shutdown() {
  $status = connection_status();

  //A connection status of 0 indicates no premature end of script
  if($status == 0){
    //Do whatever to increment the counter for the file.
  }
}
>?

There are obviously ways to improve, so if you need more details or another behaviour, please let me know!

like image 45
Konel Sum Avatar answered Sep 21 '22 09:09

Konel Sum