我使用了另一种方法,用户点击下载链接页面上的链接,而不离开当前页面,计数器实时递增。因此,不需要重定向到另一个或同一个页面,因为该文件是透明计数的,而不会离开当前页面。下载计数器imho应该是透明的,所以它更安全,没有人应该直接访问php脚本。另外请注意,我的首选方法是使用文件扩展名来限制操作,因此用户只能下载某些文件,同时请注意,更好的方法是使用.htaccess拦截匹配的文件并将它们传递给php脚本...最后,我还集成到我的项目的jQuery因此计数器在通过AJAX实时更新......所以没有必要重新加载页面看到的结果...
的.htaccess(位于根/文件/)
RewriteEngine on
RewriteRule ^(.*).(rar|zip)$ /php/doing_download.php?file=$1.$2 [R,L]
doing_download.php(位于根/ PHP)
<?php
$downloads_folder = $_SERVER['DOCUMENT_ROOT'] . '/files/';
$counters_folder = $_SERVER['DOCUMENT_ROOT'] . '/files/counter/';
if (!empty($_GET['file'])) {
$file = basename($_GET['file']);
$type = array("zip", "rar");
$exts = strtolower(substr(strrchr($file, "."), 1));
if (!in_array($exts, $type)) {
header("HTTP/1.0 403 Forbidden");
exit('File not allowed!');
} else {
if (file_exists($downloads_folder . $file)) {
if (file_exists($counters_folder . md5($file) . '_counter.txt')) {
$fp = fopen($counters_folder . md5($file) . '_counter.txt', "r");
$count = fread($fp, 1024);
fclose($fp);
$fp = fopen($counters_folder . md5($file) . '_counter.txt', "w");
fwrite($fp, $count + 1);
fclose($fp);
} else {
$fp = fopen($counters_folder . md5($file) . '_counter.txt', "w+");
fwrite($fp, 1);
fclose($fp);
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $file . '"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . sprintf("%u", filesize($downloads_folder . $file)));
$fh = fopen($downloads_folder . $file, "rb");
while (!feof($fh)) {
echo fgets($fh);
flush();
}
fclose($fh);
exit;
} else {
header("HTTP/1.0 404 Not Found");
exit('File not found!');
}
}
}
?>
count_download.php(位于根/ PHP /)
<?php
function get_download_count($file = null) {
$counters = $_SERVER['DOCUMENT_ROOT'] . '/files/counter/';
if ($file == null) return 0;
$count = 0;
if (file_exists($counters . md5($file) . '_counter.txt')) {
$fp = fopen($counters . md5($file) . '_counter.txt', "r");
$count = fread($fp, 1024);
fclose($fp);
} else {
$fp = fopen($counters . md5($file) . '_counter.txt', "w+");
fwrite($fp, $count);
fclose($fp);
}
return $count;
}
?>
call_download。PHP(位于根/ PHP)
<?php
include($_SERVER["DOCUMENT_ROOT"] . "/php/count_download.php");
$item['item1'] = get_download_count('exampleA.zip');
$item['item2'] = get_download_count('exampleB.zip');
echo json_encode($item);
?>
static_download.php(位于根/ PHP)
<?php
include($_SERVER["DOCUMENT_ROOT"] . "/php/count_download.php");
$item['item1'] = get_download_count('exampleA.zip');
$item['item2'] = get_download_count('exampleB.zip');
?>
download.js(位于根/ JSC /)
$(document).ready(function() {
$.ajaxSetup({cache: false});
getStatus();
});
function getStatus() {
$.getJSON('php/call_download.php', function(data) {
$('#item1').html(data.item1);
$('#item2').html(data.item2);
});
setTimeout("getStatus()",1000);
}
index.php(位于root /)
<?php include($_SERVER["DOCUMENT_ROOT"] . "/php/static_download.php"); ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Download Page</title>
<script type="text/javascript" src="jsc/jquery.min.js"></script>
<script type="text/javascript" src="jsc/download.js"></script>
</head>
<body>
File: <a href="files/exampleA.zip">exampleA.zip</a> - Downloaded <span id="item1"><?php echo $item['item1']; ?></span> Times<br />
File: <a href="files/exampleB.zip">exampleB.zip</a> - Downloaded <span id="item2"><?php echo $item['item2']; ?></span> Times<br />
File: <a href="test/test.zip">test.zip</a><!-- this file never will be counted since is located in other folder --><br />
</body>
</html>
根据劳伦斯Cherone和我的答案Here, you can download all files and test them!
:https://stackoverflow.com/a/29105023/4432311
PS:你可能需要改变PHP脚本文件夹...我的脚本假设你在你的服务器的根目录测试...
在header()开始之前不会回应任何内容,似乎你已经禁用了error_reporting? –
我已经禁用回声,因为它不再有用。但问题是,重定向后文件不再被下载... –
你的下载头在哪里? http://php.net/manual/en/function.header.php#example-4905 –