【发布时间】:2014-06-27 12:46:44
【问题描述】:
我创建了一个简单的 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新手。
【问题讨论】:
-
我认为正在发生的事情是,下载管理器站点使用到不同服务器的多个连接来下载相同的文件以提高速度,从而增加使用的每个服务器的计数器。我已经用谷歌搜索了您的问题,我发现的唯一可能有帮助的内容可以在此页面上找到 board.phpbuilder.com/… - 您可以使用我使用的关键字进行进一步搜索 @987654323 @ 或您认为合适的其他人。
标签: php wordpress download counter