php - 单独项目的下载计数器,但不同语言的下载计数器相同

标签 php jquery mysql ajax wordpress

我正在尝试为每个“书籍”下载创建一个计数器。

首先要注意的是,有些书籍有不同的语言下载。

我想要实现的是,如果一本书只有 1 个下载计数器,它会增加它,如果它有不同语言的下载,那么所有的下载也会只增加 1 个计数器。

计数器位于右侧: enter image description here

在这个论坛的好心人的帮助下,我成功地实现了一个按钮点击计数器,这是生成的代码:

<?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/

相关文章:

javascript - 如何使用 codeigniter 上传和移动文件 onclick 按钮 jquery?

javascript - 需要有关此切换的帮助,几乎没有变化

javascript - [已修复]试图通过使用 JS/Jquery 声明来避免重复的 CSS

javascript - JSPDF 保存在本地系统上,但我想将它保存在服务器上

php - 用 Mockery 模拟 Laravel Model::increment()

PHP MySQL - 无法回显 'TEXT' 值

php - 无法使用 PHP 将数据插入 MYSQL 表

android - 用于从 android 检索包含整数数组的参数的 mySql 代码

php - 如何将文本输出到文件?

mysql - 使用 Hibernate 将 Spring Boot 与 Mysql 集成