Link tracker/counter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

packito
Forum Newbie
Posts: 20
Joined: Mon May 19, 2003 1:53 pm

Link tracker/counter

Post 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!
dereko
Forum Newbie
Posts: 2
Joined: Mon Jun 09, 2003 10:10 am
Location: Ireland

Post 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.
packito
Forum Newbie
Posts: 20
Joined: Mon May 19, 2003 1:53 pm

Post by packito »

Ok, thanks for the tip!

I've already started to do it that way.
packito
Forum Newbie
Posts: 20
Joined: Mon May 19, 2003 1:53 pm

another problem, now using DB as suggested

Post 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());
&#125;
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?
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post 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,
packito
Forum Newbie
Posts: 20
Joined: Mon May 19, 2003 1:53 pm

Post 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! :? :?
Judas
Forum Commoner
Posts: 67
Joined: Tue Jun 10, 2003 3:34 pm
Location: Netherlands

Post 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
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
corlando
Forum Newbie
Posts: 21
Joined: Sun Jun 15, 2003 10:07 pm

Post 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
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post 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,
Judas
Forum Commoner
Posts: 67
Joined: Tue Jun 10, 2003 3:34 pm
Location: Netherlands

Post by Judas »

use session_id for tha idee ya been unique
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Judas wrote:use session_id for tha idee ya been unique
huh?

Mac
packito
Forum Newbie
Posts: 20
Joined: Mon May 19, 2003 1:53 pm

Post by packito »

ok everybody, Sorry for the delay :oops:
And thanks for the tips. They got me anxious to learn php in more detail!
packito
Forum Newbie
Posts: 20
Joined: Mon May 19, 2003 1:53 pm

Post 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!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Cookies?

Mac
Post Reply