FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


How do I count the number of downloads?





codegeek
I have a file for download on my website, and I need to count the number of times people have successfully downloaded it.
Peterssidan
I don't know how to count only successful downloads, but counting how many times a download link has been clicked is easy. You just need to link to a php page that increments a counter, in a file or in a database, and redirects to the download file.
jmraker
I think a server side detection of a successful download isn't an exact science if there isn't a client/user side file verification. A few things can go wrong on the user's side like disk errors, connection dropping, etc. It could be so common that it's the reason why on some websites the purchase of software will give you multiple download attempts.

Another solution is to analyze the web server's access log for the number of hits for the file especially if you want to retroactively get a total or a starting total for a program.
sonam
Actually I don't know how to do it but I think maybe is possible to use some Java or JavaScript to follow how many bites are downloaded. Ones, when all bites are downloaded then write in data base +1.

Sonam
codersfriend
Use a php script that lets you download a file instead of linking the file directly instead. I think this link might help
http://www.richnetapps.com/the-right-way-to-handle-file-downloads-in-php/
Then you you have created a php file, you can now add a counter that updates a file or database everytime someone downloads a file. In wordpress, some download plugins also have a counter
sonam
codersfriend wrote:
Use a php script that lets you download a file instead of linking the file directly instead. I think this link might help
http://www.richnetapps.com/the-right-way-to-handle-file-downloads-in-php/
Then you you have created a php file, you can now add a counter that updates a file or database everytime someone downloads a file. In wordpress, some download plugins also have a counter


This is quite simple with PHP but it is not simple to find successful number of downloads. Ones when you start download with PHP it is not at possible to follow user actions with PHP. User can stop, pause or cancel downloading and in that case counter will fail.

But for me this is not important. I don't care are someone download successful or not. Cool For me is enough information how many downloads are all together (successful or not successful).

Sonam
adilzulfiqar
http://tutorialzine.com/2010/02/php-mysql-download-counter/

This the best tutorial for counting the downloads.
badai
just check apache log file. it will tell you the size transfer for the file. you sum up the size, divide by the file size, you'll get the "average" number of download. the real successful download could be much lower anyway.
Arrogant
It isn't that hard. Just redirect the page to a counter php where you will include the download script. This way, the counter is incermented and download will then start.
Peterssidan
Note that for small files the whole file could be downloaded all at once, before the user has even made a choice in the open/save dialog that pops up.
codersfriend
yeah you can't tell if the user has successfully downloaded the file. But you can do a hit counter anyway for downloads
Peterssidan
If you decide to simply count "clicks" you could still try to improve it somewhat.

I think it is not uncommon that a person clicks the download link multiple times when downloading something. You could try to count all these clicks as one download. To keep track if the person has already downloaded the file you can use the IP address or a cookie (or both).

If you store the IP of all who has downloaded the file you could check each new click and only increment the counter if the IP is not already in the list. IP addresses are not fixed and can be shared by many persons so you should probably also store a timestamp and remove/ignore stored IP addresses that are too old. Using a short timeout value of 5-10 minutes is probably good enough.

You could also store if the person has downloaded the file in a cookie. The advantages with using cookies are that it doesn't matter if the IP is shared and you don't need to store anything on the server. The disadvantages are that cookies can be disabled, cleared and will not work across browsers so it will not always work and it is easy to abuse.
zimmer
that depends on what platform you are using. Most CMS is build in on that counter.
codegeek
Thank you for your answers. I learnt a lot about counting the number of downloads. I haven't really been able to find any in-depth tutorial on counting the successful downloads only, but perhaps it can be done with some JavaScript, as has been pointed out. Since I am using Wordpress, I installed a plugin called Download Manager. It is pretty useful for keeping a track of file downloads on a wordpress site.
che_in_jal
codegeek wrote:
Thank you for your answers. I learnt a lot about counting the number of downloads. I haven't really been able to find any in-depth tutorial on counting the successful downloads only, but perhaps it can be done with some JavaScript, as has been pointed out. Since I am using Wordpress, I installed a plugin called Download Manager. It is pretty useful for keeping a track of file downloads on a wordpress site.


No its not via JS at all, you can do it via .php file. Rather than directly giving the file path for downlaod, give the path for the .php file with file name as argument. then in that file you can do your count processing and give file for download via PHP headers.

http://stackoverflow.com/questions/7263923/how-to-force-file-download-with-php

You can start it from here, Let me know if you need more help.
malaz
to count the downloads follow the steps:
newfile>download.php

put this code in the download.php :



Code:
<?php

require("setup.php");
require("functions.php");
$mysql=new Mysql;
$mysql->Connect();
if ($_GET['file']) {
   $mysql->Query("SELECT * FROM fuga_counter WHERE file='".$_GET['file']."'");
   $show_file=mysql_fetch_array($mysql->result_id);
   if (!$show_file[file]) {
      $mysql->Free_result();
      $mysql->Query("INSERT INTO fuga_counter VALUES ('".$_GET['file']."','1')");
   } else {
      $mysql->Query("UPDATE fuga_counter SET downloads=downloads+1 WHERE file='".$_GET['file']."'");
   }
   header("Location: ".$_GET['file']."");
} elseif ($_GET['downloads']) {
   $mysql->Query("SELECT * FROM fuga_counter WHERE file='".$_GET['downloads']."'");
   $show_download=mysql_fetch_array($mysql->result_id);
   if ($show_download[downloads]) {
      echo ("document.writeln('$show_download[downloads]');\n");
   } else {
      echo ("document.writeln('0');\n");
   }
} else {
   echo "You didn't specify the file url";
}
$mysql->Close();


?>


Create file >functions.php
insert this code :
Code:

<?php

$version="v1.0";
if(phpversion() >= "4.2.0"){
   if(is_array($_SERVER)){
      extract($_SERVER, EXTR_PREFIX_SAME, "server");
   }
   if(is_array($_GET)){
      extract($_GET, EXTR_PREFIX_SAME, "get");
   }
   if(is_array($_POST)){
      extract($_POST, EXTR_PREFIX_SAME, "post");
   }
   if(is_array($_COOKIE)){
      extract($_COOKIE, EXTR_PREFIX_SAME, "cookie");
   }
   if(is_array($_FILES)){
      extract($_FILES, EXTR_PREFIX_SAME, "file");
   }
   if(is_array($_ENV)){
      extract($_ENV, EXTR_PREFIX_SAME, "env");
   }
   if(is_array($_REQUEST)){
      extract($_REQUEST, EXTR_PREFIX_SAME, "request");
   }
   if(is_array($_SESSION)){
      extract($_SESSION, EXTR_PREFIX_SAME, "session");
   }
}
class Mysql {
   var $mysql_username;
   var $mysql_password;
   var $mysql_db;
   var $mysql_host;
   var $result_id;
   var $link_id;
   var $dbconnect;
   var $num_rows;
   var $query_count=0;
   
   function Connect() {
      require("setup.php");
   $this->mysql_username=$mysql_username;
   $this->mysql_password=$mysql_password;
   $this->mysql_db=$mysql_db;
   $this->mysql_host=$mysql_host;
      $this->link_id=mysql_connect($this->mysql_host,$this->mysql_username,$this->mysql_password);
         if (!$this->link_id) {
         echo "Unable to connect to mysql server";
      }
      $this->dbconnect=mysql_select_db($mysql_db);
         if (!$this->dbconnect) {
         echo "Unable to select database";
      }
      return $this->link_id;
   }
   function Query($sql) {
      $this->result_id=mysql_query($sql) or die(mysql_error());
      $this->query_count++;
      return $this->result_id;
      return $this->query_count;
   }
   function Num_rows() {
      $this->num_rows=mysql_num_rows($this->result_id);
      return $this->num_rows;
   }
   function Free_result() {
      mysql_free_result($this->result_id);
   }
   function Close() {
      mysql_close($this->link_id);
   }
}


?>


create index.htm
insert this code :

Code:
<html>

<head>
<meta http-equiv="Content-Language" content="ar-sa">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>Fuga 4 Help</title>
</head>

<body>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1" dir="rtl">
  <tr>
    <td width="100%">
    <p align="center"><font face="Tahoma"><b>
    <a href="download.php?file=http:/frihost.com/myfile.rar" style="text-decoration: none">اضغط هنا</a>
    لتحميل برنامج آل <span lang="fr">ftp</span></b></font></p>
    <p align="center">
    <font face="Tahoma" size="2">عدد مرات التحميل
    <b><SCRIPT language="JavaScript" src="download.php?downloads=http:/frihost.com/myfile.rar"></SCRIPT>
   
   
 
    </b></font>
    <p align="center">
    &nbsp;</td>
  </tr>
</table>

</body>

</html>


Create install.php > insert this code :

Code:
<?php

require("setup.php");
require("functions.php");
$mysql=new Mysql;
$mysql->Connect();
$mysql->Query("CREATE TABLE fuga_counter (
  file varchar(255) NOT NULL default '',
  downloads int(11) NOT NULL default '0'
) TYPE=MyISAM");
echo "<P align=right>تم تنصيب البرنامج بنجاح";
$mysql->Close();
?>



finaly create setup.php file >insert this code

Code:
<?php
/////////////////////////////////
//  //
//              //
//            //
/////////////////////////////////
// setup.php //
///////////////
$mysql_username="database user";
$mysql_password="database password";
$mysql_db="databasename";
$mysql_host="your web url-if on pc write localhost";




?>


modify the inserted links and data in the files and save the all

Regards
Marcuzzo
necropost and serious overkill for something as simple as tracking downloads.
not to mention that this code is completely insecure and lacks extra information like 'make sure to delete the install.php' file after setting up the table'.


I would recomment using something like google analytics to track downloads
Bikerman
I have to agree with Marcuzzo.
Firstly this is a revival of a very old thread (I've not heard the term necropost before - I love it.)
Secondly this is a very convoluted solution to a simple problem.....
eltole
u can do it with javascript.
onclick effect on link that connects to a database and adds 1 to the total count. That way u can either put a count and control the downloads in a better way.
Related topics
I have a question about php
Wrong number leads to drug arrest
Asian guys can't get girls.
php coding needed
Calculating number of rows
[Edit] mySQL Count
list number of rows in a table
Downloads And Bandwidth Problems.
Why use a CMS?
Gordon Brown expresses that troops head count to go down....
How to obtain pi number
SQL - length of a number (number of digits)
PHP Array, explode, count , access array problem.
What is the greatest number imaginable???
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.