1 Replies - 1066 Views - Last Post: 23 October 2009 - 11:14 PM Rate Topic: -----

#1 Autocrat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 4
  • View blog
  • Posts: 307
  • Joined: 27-September 09

Log number of clicks on a hyperlink

Posted 23 October 2009 - 09:32 PM

I want to track the number of downloads for a file. I have linked it to the main PHP page through a simple hyperlink. I have been trying to figure out how to do so. Here's what I tried:

Create a file track.txt:


Each line represents the number of downloads for a specific file (in the order they appear on the page). I tried to create a PHP script that opens the file and increases $data[index] (where index = 1 would count for the second line of track.txt and hence the second file on the downloads page). I was soon to realize that I cannot do that with hyperlinks (as they are not form objects). How do I go about it?

Is This A Good Question/Topic? 0
  • +

Replies To: Log number of clicks on a hyperlink

#2 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 108
  • Joined: 27-February 09

Re: Log number of clicks on a hyperlink

Posted 23 October 2009 - 11:14 PM

Here's what I'd probably do to log the number of clicks for a link. First of all, I'd recommend using a database for this. Here is a basic setup for what the table could be:

Table: hyperlink_clicks

id - (primary key; auto-increment) - int
c_id - int
c_count -  (default: 0) - int

id is the unique primary key for each entry into the table. It will automatically increment. Use it to reference every entry uniquely and individually.

c_id stands for "click id" and is the unique id you assign for each link that is clicked. E.g. say we have a link for a download called "Download A." You might assign "Download A" to a unique id of "1" and "Download B" to "2."

c_count is the number of clicks on each link.

As you can see, all 3 values are integers. I'd make them each 11 in the length (or whatever you think is sufficient).

Now for the actual code. Say you store downloads in location "downloads/" on your server. And say you have 2 downloads, one called "downloada.zip" and one called "downloadb.zip."

Instead of making a link that redirects the user to "downloads/downloada.zip", create a php file called "process_click.php" or something, and send some get values in. Here is an example of what I mean:

For "downloada.zip":
<a href="process_click.php?cid=1&link=downloada.zip">Download A</a>

We pass in ?cid as the unique identification number for each download, and as for the &link, that is for the actual location of the file to be downloaded inside the "downloads/" folder (or whatever).

Now, the actual PHP page process_click.php would just take the cid from the URL using $_GET["cid"] and then UPDATE the database setting the value for c_click to it's own value plus one.

After it updates the click value, it would grab the value of &link and redirect the user to the desired page based on its value, e.g.

$link = mysql_real_escape_string($_GET["link"]);
if ($link)
header("Location: downloads/" . $link);

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1