我正在尝试为每个“书籍”下载创建一个计数器。
首先要注意的是,有些书籍有不同的语言下载。
我想要实现的是,如果一本书只有 1 个下载计数器,它会增加它,如果它有不同语言的下载,那么所有的下载也会只增加 1 个计数器。
计数器位于右侧:
在这个论坛的好心人的帮助下,我成功地实现了一个按钮点击计数器,这是生成的代码:
<?php
$counterFile = 'counter.txt' ;
// jQuery ajax request is sent here
if ( isset($_GET['increase']) )
{
if ( ( $counter = @file_get_contents($counterFile) ) === false ) die('Error : file counter does not exist') ;
file_put_contents($counterFile,++$counter) ;
echo $counter ;
return false ;
}
if ( ! $counter = @file_get_contents($counterFile) )
{
if ( ! $myfile = fopen($counterFile,'w') )
die('Unable to create counter file !!') ;
chmod($counterFile,0644);
file_put_contents($counterFile,0) ;
}
?>
<script type="text/javascript">
jQuery(document).on('click','a#download',function(){
jQuery('div#counter').html('Loading...') ;
var ajax = jQuery.ajax({
method : 'get',
url : '/test.php', // Link to this page
data : { 'increase' : '1' }
}) ;
ajax.done(function(data){
jQuery('div#counter').html(data) ;
}) ;
ajax.fail(function(data){
alert('ajax fail : url of ajax request is not reachable') ;
}) ;
}) ;
</script>
<div id="counter"><?php echo $counter ; ?></div>
<a href="<?php echo get_field("pdf"); ?>" id="download" onclick="window.open(this.href);return false;">Download btn</a>
此解决方案的问题在于,它对所有书籍进行相同的计数,它不会单独下载每本书。
我找到了这个教程:http://demo.tutorialzine.com/2010/02/php-mysql-download-counter/demo.php 但我看到的问题是,它对每个文件下载进行计数,因此,如果我将其应用到我的网站,那么具有不同语言的书籍将计入不同的数字。
我怎样才能让它按照我想要的方式工作?就像我说的,如果一本书只有 1 个下载计数器,它会增加它,如果它有不同语言的下载,那么所有的下载也会在 1 个计数器中增加。
最佳答案
执行此操作的最佳方法肯定是使用数据库,但如果您想使用文件执行此操作,下面的方法应该可以解决问题。
类 DownloadCount
处理所有计数器操作,并将计数器信息存储在 JSON 文件中,按图书文件名索引:
get()
返回指定图书的下载计数。update()
增加指定图书的下载计数并返回新的下载计数。save()
将任何计数器更改写入文件。
请注意,这并不稳健,因为同时请求可能会相互干扰(这就是首选数据库的原因)。
<?php
class DownloadCount
{
protected $_file;
protected $_counters;
public function __construct($file)
{
if (file_exists($file)) {
$this->_file = $file;
$this->_counters = json_decode(file_get_contents($file), true);
if (empty($this->_counters)) $this->_counters = [];
} else {
die('Error : file counter does not exist');
}
}
public function get($book)
{
$book = basename($book);
return (array_key_exists($book, $this->_counters)
? $this->_counters[$book]
: 0);
}
public function update($book)
{
$book = basename($book);
if (array_key_exists($book, $this->_counters)) {
$this->_counters[$book]++;
} else {
$this->_counters[$book] = 1;
}
return $this->_counters[$book];
}
public function save()
{
file_put_contents($this->_file, json_encode($this->_counters), LOCK_EX);
}
}
$count = new DownloadCount('./counter.json');
if (!empty($_GET['increase'])) {
// this is the ajax response of the incremented count.
echo $count->update($_GET['increase']);
$count->save();
exit;
} else {
$pdf = get_field('pdf');
?>
<script type="text/javascript">
jQuery(document).on('click', 'a#download', function()
{
jQuery('div#counter').html('Loading...');
console.log(this.href);
var ajax = jQuery.ajax({
method : 'get',
url : '/test.php', // Link to this page
cache: false,
data : { 'increase' : this.href }
});
ajax.done(function(data)
{
jQuery('div#counter').html(data);
});
ajax.fail(function(data)
{
alert('ajax fail : url of ajax request is not reachable');
});
});
</script>
<div id="counter"><?php echo $count->get($pdf); ?></div>
<a href="<?php echo $pdf ?>" id="download" onclick="window.open(this.href);return false;">Download btn</a>
<?php
}
关于php - 单独项目的下载计数器,但不同语言的下载计数器相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26926415/