当使用像IDM这样的下载管理器时,下载计数器增加三倍


download counter increases by thrice when using a download manager like IDM

我创建了一个简单的php下载计数器,在点击下载链接后,捕获并保存详细信息,如ip,文件名,下载次数等。到目前为止一切都很好。Counter也会随着每次下载而增加,但当使用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新手。

IDM通过请求同一文件的不同部分来并行下载。

您应该尝试检查$_SERVER['HTTP_RANGE']变量是否存在,如果存在则不要增加计数器。这个变量被类似idm的软件或浏览器用来恢复下载。