Page 1 of 1

hyperlink click counter

Posted: Mon Apr 14, 2003 9:26 am
by Lukenet
Hi all,

I know this may seem basic, but I am just looking for different ways to tackling this problem. (Yes I am a Newbie)

Here are the specs.
---------------------------------------
I want to count how many times someone clicks on a link to an external site.

I want to store that information (the number of clicks) in a DB (MySQL) so I can retrieve it later and use it for things like top 5 clicked links etc..

I want to make the method of collecting the information as transparent to the end user as possible.

*Extra info*
I want to store the link info i.e. the URL and link text in the DB as well.
----------------------------------------

Well what do you think? I am looking for a number of different ways to do this. I will probably use / learn / develop a solution from the simplest, most elegant suggestion that is also the most transparent to the end user.

The project is just for me, I want to make a link page for my favourite sites (URLs) and learn some PHP in the process.

Lukenet

Posted: Mon Apr 14, 2003 12:52 pm
by Jim
I can't believe I gave you stupid code like that. I must have been very, very tired...

Try this:

Create a table called "link_count" or something, with the fields:

link (this is the url of the site)
hits (this is the number of hits the site has received)
site (the name of the site you link to [you said you wanted this])

When you link to a site, create a query string like this:

http://www.yoursite.com/link.php?site=h ... ink-to.com

Put this code on link.php:

Code: Select all

<?

/****  Link.php  ****/

$link = $_GET['link'];

$sql = "select hits from link_count where site = '$site'";
$act = mysql_query($sql);

while($hits = mysql_fetch_array($act)) {

$hits = $hits['hits'];

}

$sql = "update link_count set hits = '$hits+1'";
$act = mysql_query($sql);
}

?>

That should add 1 to "hits" on your table.


To call the script, create a page like the following:

Code: Select all

<?

//We'll call this list.php

$sql = "select site, hits from link_count order by hits asc";
$act = mysql_query($sql);

while($data = mysql_fetch_array($act)) {

$hits = $data['hits'];
$site = $data['site'];
$link = $data['link'];

echo "<b><a href='$link'>$site</a></b> has received $hits hits so far!";
}


?>

That ought to work. Once again, it's untested. Sorry about my stupid idea before this. :P