Ezilon.com - Target Your Audience, be Seen in Your Region

PHP Script to log link clicks

You are viewing this site as a guest. Join our community to get your questions answered and share knowledge. Active members may advertise and ask for a website critique.

He has: 107 posts

Joined: Mar 2006

I want to log link clicks using a database on my website to display what user clicked on the link and the number of times that link has been clicked etc

I have a basic idea of how this might work,

Something like this?

Page but not sure about how to get the number of times the link has been clicked,

Also, what if the link is to an external website, and how do I record the number of times the link has been clicked as well as the user who clicked it?

Renegade's picture
Moderator

He has: 2,943 posts

Joined: Oct 2002

What you could try doing is a sort of redirect thing:
Page

So, in redirect.php, have something that counts how many times that page was visited (might want to check where it's from too) as well is anything else you want to add to the database then at the very end, redirect to the page it's supposed to go to.

All this happens in about 2 seconds so, users don't really notice it much.

Cheng Eu Chew - Renegade
Download:
- Mozilla, Firefox
- Opera
This post may contain peanut traces

He has: 107 posts

Joined: Mar 2006

That sounds like a good option Cool how would I do that? I'm guessing using java script maybe? or asp/php would do the same?

I think your redirect page idea is prob the best way of doing it, I had an idea last night but I think it would be difficult counting the number of visists from each user to a particular link. My idea was:

On the page where the link is, get the current username thats logged in using a querystring (link.asp?user=username) Question: Can multiple querystrings be sent? for example, ?user=username, ?link=link, etc)

That username is sent to another page which asks the user if they want to continue following that link, if the user clicks 'Ok' the username is added to the database and then if I want the number of times that user clicked on the link I use row count where user=username?

Back to the redirect page idea

Before the end part that redirects the user to the page they want to see, I would need something that stores:

The total views
The users who have clicked on the link
The number of times each user has clicked on the link

I'm going to research how to start going about this but any help or code examples will be very much appresiated! Wink

Renegade's picture
Moderator

He has: 2,943 posts

Joined: Oct 2002

Not entirely sure how it would be done, but I have seen similar things inplemented several times, even here at TWF.

Logging what you want is up to you, and can't really help too much on that but, I can say that at the end, all you really need is the following:

<?php
header
("Location: " . $url);
?>

Cheng Eu Chew - Renegade
Download:
- Mozilla, Firefox
- Opera
This post may contain peanut traces

He has: 8 posts

Joined: Aug 2007

You just need to write a little redirect script in PHP then send all links via that script. In code redirect.php would look something like this:

<?PHP

// decode the location you want to visit
$loc = base64_decode($_GET['loc']);

// update the number of times this particular location has been visited in your database
$sql = "UPDATE page_visits SET visits=visits+1 WHERE loc=".$loc." LIMIT 1";
$result = mysql_query($sql);

header('Location: '.$loc);

?>

Then you call redirect.php like this:

">Visit Google

You'll obviously need to have a MySQL database setup with a table called page_visits with columns for visits and loc.

I also encoded the url to make it look neater and less prone to parsing errors. I base64_encoded it because I like to be secretive sometimes.

Disclaimer: This is untested code and if it wipes you computer and starts global armageddon you can't blame me!