Create PHP Download Counters


Sometimes, maybe you want to know how popular your programs are. You put the download links but have no idea how many of them have been downloaded. In fact, it is very easy to do this since PHP+MySQL is very popular and provided by most of the web-hosting companies.

Today, I spent less than one hour creating a few PHP pages which have enabled this download-counter feature easily for the future links. I would like to share with you.

First of all, you have to create a table e.g. ‘counters’ to store the links and their download counters. It looks like below.

counter Create PHP Download Counters implementation mysql php technical

The ‘CounterID‘ is the primary key and made auto-incremented. The URL stores the download link e.g. “https://steakovercooked.com/../../a.zip” and finally the ‘Counter‘ is the downloaded counter.

The next thing is to write a class which wraps a few functions to access this table. For example,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
  // https://helloacm.com
  // make sure the MySQL links are ready..
  class Counter
  {
    private $id = 0;
    private $url = "";
    private $counter = 0;
    
    function Counter($_id)
    {
      $this->Load($_id);         
    }
    
    public function GetID()
    {
      return ($this->id);
    }
    
    public function Load($_id)
    {
      $i = (integer)$_id;
      $query = "
        select *
        from `counters`
        where
        `CounterID` = '$i'
      ";
      $result = mysql_query($query);
      if ($result)
      {
        $num_rows = mysql_num_rows($result);
        if ($num_rows > 0)
        {
          $row = @mysql_fetch_array($result);
          $this->id = $_id;
          $this->url = $row['URL'];
          $this->counter = $row['Counter'];
          return (true);
        }
      }
      return (false);
    }
    
    public function Save()
    {
      $uid = (integer)$this->id;
      $url = mysql_real_escape_string($this->url);
      $counter = (integer)$this->counter;
      
      $query = "update 
        `counters`
        set
            `URL` = '$url',
            `Counter` = '$counter'
        where
            `CounterID` = '$uid'
      ";
      
      if (mysql_query($query))
      {
        return (true);
      }
      return (mysql_error());      
    }
  
    public function GetURL()
    {
      return ($this->url);
    }
    
    public function SetURL($url)
    {
      $this->url = $url;
    }
    
    public function GetCounter()
    {
      return ($this->counter);
    }
    
    public function SetCounter($cnt)
    {
      $this->counter = $cnt;
    }
    
    public function ClearCounter()
    {
      $this->SetCounter(0);
    }
    
    public function IncCounter()
    {
      $this->counter ++;
    }
    
    public function DecCounter()
    {
      $this->counter --;
    }
  }
?>
<?php
  // https://helloacm.com
  // make sure the MySQL links are ready..
  class Counter
  {
    private $id = 0;
    private $url = "";
    private $counter = 0;
    
    function Counter($_id)
    {
      $this->Load($_id);         
    }
    
    public function GetID()
    {
      return ($this->id);
    }
    
    public function Load($_id)
    {
      $i = (integer)$_id;
      $query = "
        select *
        from `counters`
        where
        `CounterID` = '$i'
      ";
      $result = mysql_query($query);
      if ($result)
      {
        $num_rows = mysql_num_rows($result);
        if ($num_rows > 0)
        {
          $row = @mysql_fetch_array($result);
          $this->id = $_id;
          $this->url = $row['URL'];
          $this->counter = $row['Counter'];
          return (true);
        }
      }
      return (false);
    }
    
    public function Save()
    {
      $uid = (integer)$this->id;
      $url = mysql_real_escape_string($this->url);
      $counter = (integer)$this->counter;
      
      $query = "update 
        `counters`
        set
            `URL` = '$url',
            `Counter` = '$counter'
        where
            `CounterID` = '$uid'
      ";
      
      if (mysql_query($query))
      {
        return (true);
      }
      return (mysql_error());      
    }
  
    public function GetURL()
    {
      return ($this->url);
    }
    
    public function SetURL($url)
    {
      $this->url = $url;
    }
    
    public function GetCounter()
    {
      return ($this->counter);
    }
    
    public function SetCounter($cnt)
    {
      $this->counter = $cnt;
    }
    
    public function ClearCounter()
    {
      $this->SetCounter(0);
    }
    
    public function IncCounter()
    {
      $this->counter ++;
    }
    
    public function DecCounter()
    {
      $this->counter --;
    }
  }
?>

The ‘Counter‘ class can be easily used to retrieve counter/url given a download ID (which is unique).

The following showcnt.php will increment the counter according to the ID and redirect to the URL.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
  // https://helloacm.com  
  $id = 0;
  if (isset($_GET['id']))
  {
    $id = (integer)($_GET['id']);
  }
  if ($id > 0)
  {
    require_once('class.counter.php');
    $obj = new Counter($id);
    if ($obj->GetID() > 0)
    {
        $obj->IncCounter();
        $obj->Save();
        header("Location: ".$obj->GetURL());
        die();
    }
    else
    {
        header("http/1.1 404 Not Found"); 
        die();  
    }
  }
?>
<?php
  // https://helloacm.com  
  $id = 0;
  if (isset($_GET['id']))
  {
    $id = (integer)($_GET['id']);
  }
  if ($id > 0)
  {
    require_once('class.counter.php');
    $obj = new Counter($id);
    if ($obj->GetID() > 0)
    {
  		$obj->IncCounter();
  		$obj->Save();
  		header("Location: ".$obj->GetURL());
  		die();
    }
    else
    {
  		header("http/1.1 404 Not Found"); 
	   	die();  
    }
  }
?>

The next thing is to show the counter in the HTML pages. Generate a counter image and display in the HTML img tag will be the easiest. Or you can simply print the counter and using Ajax to dynamically retrieve the contents of the counter-printing pages. Other easy approaches would be to use frames or iframe to include this page. The image generating is the most handy solution.

The following displays the counter in an PNG image which you can easily include in the HTML page.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
  // https://helloacm.com
  $id = 0;
  if (isset($_GET['id']))
  {
        $id = (integer)($_GET['id']);
  }
  if ($id > 0)
  {
        require_once('class.counter.php');
        $obj = new Counter($id);
  }
  else
  {
        header("http/1.1 404 Not Found"); 
        die();
  }
  if (0 == $obj->GetID())
  {
        header("http/1.1 404 Not Found"); 
        die();  
  }
  header("Content-type: image/PNG"); 
  if (!isset($_GET["width"]))
  {
        $width = 50;
  }
  else
  {
        $width = $_GET["width"];
  }
  if (!isset($_GET["height"]))
  {
        $height = 20;
  }
  else
  {
        $height=$_GET["height"];
  }
  if (!isset($_GET["font"]))
  {
        $font = 5;
  }
  else
  {
        $font = $_GET["font"];
  }
  $im = @imagecreate($width,$height); 
  $white = ImageColorAllocate($im, 255,255,255);
  $textcolor = ImageColorAllocate($im, 0,0,0); 
  imagestring($im, $font, 7, 3, $obj->GetCounter(), $textcolor); 
  ImagePNG($im); 
  ImageDestroy($im);
?> 
<?php
  // https://helloacm.com
  $id = 0;
  if (isset($_GET['id']))
  {
		$id = (integer)($_GET['id']);
  }
  if ($id > 0)
  {
		require_once('class.counter.php');
		$obj = new Counter($id);
  }
  else
  {
		header("http/1.1 404 Not Found"); 
		die();
  }
  if (0 == $obj->GetID())
  {
		header("http/1.1 404 Not Found"); 
		die();  
  }
  header("Content-type: image/PNG"); 
  if (!isset($_GET["width"]))
  {
		$width = 50;
  }
  else
  {
		$width = $_GET["width"];
  }
  if (!isset($_GET["height"]))
  {
		$height = 20;
  }
  else
  {
		$height=$_GET["height"];
  }
  if (!isset($_GET["font"]))
  {
		$font = 5;
  }
  else
  {
		$font = $_GET["font"];
  }
  $im = @imagecreate($width,$height); 
  $white = ImageColorAllocate($im, 255,255,255);
  $textcolor = ImageColorAllocate($im, 0,0,0); 
  imagestring($im, $font, 7, 3, $obj->GetCounter(), $textcolor); 
  ImagePNG($im); 
  ImageDestroy($im);
?> 

The working example can be found in https://helloacm.com/script32-a-freeware-to-convert-your-vbscriptjscript-to-executables/ where the downloading of two applications are monitored.

It is also a good advice to put a nofollow value to the ‘rel‘ attribute in the [a href] tag. For example, [a href=’link’ rel=’nofollow’]download[/a] tells search engines not to follow the link.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
764 words
Last Post: Advanced DNS: Terms Explained
Next Post: Codeforces: A. Hexadecimal's theorem

The Permanent URL is: Create PHP Download Counters

Leave a Reply