我创建了一个简单的php下载计数器,在点击下载链接后,捕获并保存详细信息,如ip,文件名,没有下载已经完成等等。到目前为止一切正常。对于每次下载,计数器也会增加,但是当使用IDM(或类似的下载管理器)时,问题会弹出; 然后每次下载时计数器增加三倍!
我使用的代码看起来像这样(在WordPress环境中) -
global $wpdb;
$db_table_name = $wpdb->prefix. 'test_downloads_info';
$sql = "SELECT download_count FROM $db_table_name WHERE download_name = %s";
$result = $wpdb->get_row($wpdb->prepare($sql, $download_name), ARRAY_A);
// If the row fetched has values..
if(!empty($result) && !empty($result['download_count'])){
// Increment the counter by one..
$download_count = $result['download_count'] + 1;
// And update the corresponding row..
$update_result = $wpdb->update($db_table_name, array('download_ip'=> $download_ip, 'download_count'=> $download_count, 'download_date'=> $download_date), array('download_name'=> $download_name));
}
// Otherwise..
else{
// Insert the new records..
$insert_result = $wpdb->insert($db_table_name, array('download_name'=> $download_name, 'download_url'=> $download, 'download_ip'=> $download_ip, 'download_count'=> $download_count, 'download_date'=> $download_date), array('%s', '%s', '%s', '%d', '%s'));
}
/**
* Prepare to force download the requested file
*/
// Required for IE..
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
// Send all the needed headers for download prompting..
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate; post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: application/pdf');
header("Content-Disposition: attachment; filename=\"". basename($download). "\"");
header('Content-Transfer-Encoding: binary');
/*header('Content-Length: '. filesize($download));*/
header('Connection: close');
readfile($download);
exit();
}
所以主要的问题是,如果使用像IDM这样的下载管理器,计数器会增加两倍或三倍。我不知道我在这里做错了什么。另外,我是一个php新手。
答案 0 :(得分:0)
IDM通过请求同一文件的不同部分来并行化下载来工作。
您应该尝试检查$_SERVER['HTTP_RANGE']
变量是否存在,如果是,则不要递增计数器。此变量由类似IDM的软件使用,或由浏览器用于恢复下载。