Page 1 of 2
Link tracker/counter
Posted: Mon Jun 09, 2003 10:14 am
by packito
Hi!
I need help with putting a link tracker/counter on my website.
I have a page where people can download a certain type of files, and want to keep track on how many times each file has been downloaded. (like the number of times each topic has been seen on this forum.)
I intend to keep a .txt file for each link, containing the number of times it has been downloaded. Every time a person clicks on a link (e.g. mylink1.whatever) the script must open the associated text file (e.g. mylink1.txt) read it contents, which is only a number, and increment that number by 1. Also, the links are to be put on a table.
Until now i'm only able to open the associated .txt file and display the number in it (users must know if a certain file is a good choice). So, you can imagine how newbie i am...
Can anyone help me with this subject? Also, it it possible to use just one .txt (or .dat, whatever) file for all links??
Thanks!
Posted: Mon Jun 09, 2003 10:34 am
by dereko
I think it would be allot easier if you used a db (MySQL for example) to store the link counter instead of a file
You could have a table like
URL | Counter
link1 | 2
Link2 |34
....
then just create a php file with that automatically increments the counter field of the relavent link
then just include the file and set the your link string so it only increments the relevent link counter.
Posted: Fri Jun 13, 2003 3:31 pm
by packito
Ok, thanks for the tip!
I've already started to do it that way.
another problem, now using DB as suggested
Posted: Fri Jun 13, 2003 6:01 pm
by packito
Ok Dereko, folks... now i have another problem!
This is the code i use to show how many hits there are for each link:
Code: Select all
function showCount($filename){
mysql_select_db($database_connRapMania, $connRapMania);
$result = mysql_query("SELECT contagem FROM link_count WHERE ficheiro='$filename'", $connRapMania) or die(mysql_error());
if (mysql_num_rows($result)){
$pontos=mysql_fetch_row($result);
//$pontosї0] -=1;
print ("$pontosї0]");
}
}
And this the code i use to update links in the link_count table, which only has 2 columns:
ficheiro - contains each file's entry name in the database
contagem - (for each link) saves the number of hits
Code: Select all
function updateCount($filename, $linktext, $linkDB){
$path="../sacanco";
print ("<a href="$path/$filename">$linktext</a");
mysql_select_db($database_connRapMania, $connRapMania);
$update = mysql_query("UPDATE link_count SET contagem=contagem+1 WHERE ficheiro='$linkDB'", $connRapMania) or die(mysql_error());
}
The problem is that every time i reload the page, the number in contagem is automatically incremented by 1, thus function showCount never shows the correct number of hits for each link.
I must only run function updateCount on mouse click, right? How can i do this?
Posted: Fri Jun 13, 2003 7:08 pm
by cactus
You need to find something unique about every user of your site/service, the obvious one is the referer IP address ($_SERVER["REMOTE_ADDR"]) of your users.
Write this to a new feild in your dB, when the page refreshes you need to do a quick in your dB to see if the IP you have from the user matches that in the dB, if it does then
don't increment the counter.
There are a few other things you could do to ensure your hit's are more unique, but I'll leave that for you to figure out
Regards,
Posted: Sun Jun 15, 2003 2:58 pm
by packito
Sorry Cactus, but could you be more specific? I don't understand how adding such a field can solve the problem... now i'm totally confused!

Posted: Sun Jun 15, 2003 8:30 pm
by Judas
uuh dude packito
try working with a session layer
where you admin your user vars and where from you write to a data field Ok.
sample.
sesssion_start();
if (!empty($user_var['session'])){user_var['session']=session_id();}
if (!in_array($user_var['link'],$link)){$user_var['link'][]=$link;}
etc......
global $user_var;
then you can tricker a php script when logout of ya page with a onUnload event in your head tag do this in hybride style
or some thing like that
Posted: Sun Jun 15, 2003 9:13 pm
by McGruff
cactus wrote:You need to find something unique about every user of your site/service, the obvious one is the referer IP address ($_SERVER["REMOTE_ADDR"]) of your users.
$_SERVER['REMOTE_ADDR'] is not a reliable way to identify users who may have dynamic IPs.
Posted: Sun Jun 15, 2003 10:07 pm
by corlando
maybe this will help...
Code: Select all
<?php
// getFile.php
session_start();
function incrementFileCounter( $linkURL = "" ) {
/*
use whatever counting system you want here
example
-------
mysql_query("UPDATE link_table SET cnt=cnt+1 WHERE link=$linkURL");
*/
}
$linkURL = $_GET['linkURL'];
if ( ! isset($_SESSION['linksArray']) ) {
$_SESSION['linksArray'] = array($linkURL);
incrementFileCounter( $linkURL );
}
elseif (! in_array($linkURL, $_SESSION['linksArray']) ) {
array_push( $_SESSION['linksArray'], $linkURL );
incrementFileCounter( $linkURL );
}
// Forward browser to location of file
header("Location: $linkURL");
exit();
?>
example of the calling url would be...
Code: Select all
getFile.php?linkURL=helloworld.zip
Posted: Mon Jun 16, 2003 6:41 am
by cactus
McGruff wrote:cactus wrote:You need to find something unique about every user of your site/service, the obvious one is the referer IP address ($_SERVER["REMOTE_ADDR"]) of your users.
$_SERVER['REMOTE_ADDR'] is not a reliable way to identify users who may have dynamic IPs.
Hence:
cactus wrote:There are a few other things you could do to ensure your hit's are more unique, but I'll leave that for you to figure out
Regards,
Posted: Mon Jun 16, 2003 9:53 am
by Judas
use session_id for tha idee ya been unique
Posted: Mon Jun 16, 2003 10:16 am
by twigletmac
Judas wrote:use session_id for tha idee ya been unique
huh?
Mac
Posted: Sat Jul 12, 2003 2:59 pm
by packito
ok everybody, Sorry for the delay
And thanks for the tips. They got me anxious to learn php in more detail!
Posted: Fri Oct 24, 2003 5:33 pm
by packito
Hi, it's me again!
I've solved this problem using a database to store the users IP. I use the same process to register the users votes on a certain poll, but recently i've received a lot of complains of people who have never voted but still aren't able to do so! The problem, i believe, is due to shared IP's and i don't know how to solve it!
cactus wrote:
There are a few other things you could do to ensure your hit's are more unique, but I'll leave that for you to figure out

Regards,
Cactus, or anyone else, could you enlight me??
Thanks!
Posted: Mon Oct 27, 2003 3:03 am
by twigletmac
Cookies?
Mac