PHP making a download counter without leaving the current page

前端 未结 4 1353
刺人心
刺人心 2020-12-11 11:31

I tried to create a download counter with PHP. The script, I have created, works, but when I click the download link, the script sends me to a blank page. Is it possible to

4条回答
  •  半阙折子戏
    2020-12-11 12:15

    I'd like to improve the answer of Lawrence Cherone. The download counter, imho, should be transparent so it's more secure, nobody should have access directly to php scripts. Finally I integrated jquery so the counter is updated in real time via ajax... so there is no need to reload the page to see the results...

    .htaccess (located in root/files/)

    RewriteEngine on
    RewriteRule ^(.*).(rar|zip)$ /php/doing_download.php?file=$1.$2 [R,L]
    

    doing_download.php (located in root/php)

    
    

    count_download.php (located in root/php/)

    
    

    call_download.php (located in root/php)

    
    

    static_download.php (located in root/php)

    
    

    download.js (located in root/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 (located in root/)

    
    
    
    
    
    Download Page
    
    
    
    
    File: exampleA.zip  -  Downloaded  Times
    File: exampleB.zip  -  Downloaded Times
    File: test.zip

    Here, you can download all files and test them!

    I hope my answer based on answer of Lawrence Cherone will solve any problem :) @Lawrence Cherone: great job!!!

提交回复
热议问题