How to count clicks on link, download a file and update a database on php

I have a mysql database based php script,
In some page a have a download button, I would like to count the clicks on the button and at the same time:

  1. To update the database and
  2. To allow the download of a file

Can any one give me some directions? I’ll appreciate a lot

I would appreciate really if someone could give me some directions on how to do this ?
thanks a lot

Hi Claudia_Nenas, (a belated) welcome to the forum

I’m assuming the button is in a form or causes a new page to load?

I think the simplest means of tracking downloads would be using google analytics avoiding a proprietary solution.

I think I cannot use external softeware or providers like g analitics. I just need to track a variable (downloads) add 1 to that variable , allow a file to doownload and update a value on a database table.

Quaestion, do you know how to pass 3 variables in a URL link ? I have seen the case with two variables and for some reason it doesn’t work with three.


Well there is a button to download a file. (the page is actually a url to download a file), so I need that when someone click on that button the file get downloaded and a variable get increased by 1 then at the end a sql will add this new value to a dabase, all in php.


Can you explain to me the advantage of doing it that way instead of using a proven tool such as google analytics. Google analytics is an industry standard for click tracking and logging user behavior.

well the reason I think is that I have let say an application store and there is a button to download an app for any application published on the store. Is possible for google analytics to track the counts of every single app? Thnik about googlplay I want to do something simmilar and tracj the downloads of every app.


I was thinking in something local and not to depend on third parties to do it. and taking advantage of the powers of php. Regards

Yes, Google analytics can do that and much more.

What advantage does that offer?

You know what? I don’t want to depend on Google, specially when this will be for another android app store. I’m pretty sure I can do it just with php is just that I’m a beginner but I’m pretty sure I will find the way with a little support and a lot of study. Thanks any way

I know GA can work with pages very well. I don’t know if it can collect data about files that aren’t pages. Quite possible, just that I’ve never needed to look into it.

But if the form is submitting to a file it should be easy enough to put your database code there.


I’m pretty sure it can be done with php, unfortunately I’m just a php beginner. I just need to know how to pass Three variables in a url link to use on a php file. Can you help on this ?

Thanks a lot

If it’s a form they’re most likely$_POST variables. if in the URL they’re $_GET variables. Both are global arrays.

If you temporarily put


at the top of the PHP file what do you when you see when you end up there?

Google Analytics does not track users who have either JavaScript or Google Analytics turned off. It is a statistics program that gives approximate counts. If you need exact counts then you need your own server side solution that counts every single access and doesn’t provide a way to do the access that doesn’t get counted.

In my experience those stats are good enough. Not to mention Google analytics has a more sophisticated algorithm for detecting malicious behavior. One can always use the server side api as well. Though caching has a potential to affect the accuracy of that.

Well I was able to pass 3 variables in a url using the following code:

$vars = array('downloads' => $desca, 'id' => $ident, 'url'=>$url);
$querystring = http_build_query($vars);
$url2 = "". $querystring;

En the link to download the file is:

<a href="<?php echo $url2 ?>" target="_blank">DOWNLOAD FILE

Now these things are already happening:
1 The downloads variable is increased by 1 and the database fields are updated with the final downloads value.

My only issue now is how can I redirect the browser to the download file which the URL $url after the database is updated?

Thanks in advance for your support

oooppss the page interpreted the html code

the download file url is
< a href=“<?php echo $url2 ?>” target=“_blank”>DOWNLOAD2