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.
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.
我在文件托管网站上实施了类似的解决方案。
您想要做的是使用 register_shutdown_function 回调,它允许您检测 php 脚本的执行结束,而不管结果如何。
然后,您希望将文件放在服务器上不可通过网络访问的位置,并通过 php 进行下载:想法是您希望能够跟踪已传递给客户端的字节数。
这是实现的基本方式(例如:)
<?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.
}
}
>?
显然有改进的方法,所以如果您需要更多细节或其他行为,请告诉我!
如果您可以监控 Web 服务器返回的 HTTP 响应代码并将它们与生成它们的会话联系起来,那么您就可以做生意了。
响应代码 206 表明系统已传递了部分信息,但不是全部。当文件的最后一个块发出时,它不应该有 206 的响应代码。
如果您可以通过将会话代码放在 URL 中来将其与用户会话联系起来,那么您可以根据简单的日志聚合给出分数。