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

Link tracker/counter

Postby packito » Mon Jun 09, 2003 10:14 am

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

Postby dereko » Mon Jun 09, 2003 10:34 am

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

Postby packito » Fri Jun 13, 2003 3:31 pm

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

Postby packito » Fri Jun 13, 2003 6:01 pm

Ok Dereko, folks... now i have another problem!

This is the code i use to show how many hits there are for each link:

Syntax: [ Download ] [ Hide ]
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

Syntax: [ Download ] [ Hide ]
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?
packito
Forum Newbie
 
Posts: 20
Joined: Mon May 19, 2003 1:53 pm

Postby cactus » Fri Jun 13, 2003 7:08 pm

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

Postby packito » Sun Jun 15, 2003 2:58 pm

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

Postby Judas » Sun Jun 15, 2003 8:30 pm

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

Postby McGruff » Sun Jun 15, 2003 9:13 pm

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

Postby corlando » Sun Jun 15, 2003 10:07 pm

maybe this will help...


Syntax: [ Download ] [ Hide ]
<?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...
Syntax: [ Download ] [ Hide ]
getFile.php?linkURL=helloworld.zip
corlando
Forum Newbie
 
Posts: 21
Joined: Sun Jun 15, 2003 10:07 pm

Postby cactus » Mon Jun 16, 2003 6:41 am

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

Postby Judas » Mon Jun 16, 2003 9:53 am

use session_id for tha idee ya been unique
Judas
Forum Commoner
 
Posts: 67
Joined: Tue Jun 10, 2003 3:34 pm
Location: Netherlands

Postby twigletmac » Mon Jun 16, 2003 10:16 am

Judas wrote:use session_id for tha idee ya been unique

huh?

Mac
User avatar
twigletmac
Her Royal Site Adminness
 
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Postby packito » Sat Jul 12, 2003 2:59 pm

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

Postby packito » Fri Oct 24, 2003 5:33 pm

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

Postby twigletmac » Mon Oct 27, 2003 4:03 am

Cookies?

Mac
User avatar
twigletmac
Her Royal Site Adminness
 
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Next

Return to PHP - Code

Who is online

Users browsing this forum: dimxasnewfrozen, Google [Bot] and 1 guest