Forum Moderators: open

Message Too Old, No Replies

Counting outbound clicks

         

madk

7:07 pm on Jun 29, 2007 (gmt 0)

10+ Year Member



Hello all,

I've finally come up with a solution to count my outbound clicks without using another file to forward. I wanted to post it here before I went live with it to see if anyone had any suggestions for improvement. I just needed a way to count the clicks while still retaining the original URL so that it holds its value.

Code:

test.html
---------


<a href="http://www.example.com" onclick="parent.location='counter.php?url=http://www.example.com'" target="_blank">Example.com</a>

counter.php
-----------


// Connect to DB
include_once('config.php');
// Get the URL from the variable
$url = $_GET['url'];
// Update the counter
$update = "UPDATE links SET counter = counter + 1 WHERE site_url = '$url'";
$result = mysql_query($update);
// Return to previous page
echo '<script type="text/javascript">history.go(-1);</script>';

Thank you,
M Kris

Fotiman

7:21 pm on Jun 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm thinking that you'd be better off using AJAX instead of hitting a new page and then going back. For one thing, I would suspect this might cause some problems with the "Back" and "Forward" functionality of the browser. For example, after this update occurs in your example, the user would be able to click "Forward" in their browser, which would visit your counter.php file again, incrementing the count erroneously.

madk

7:28 pm on Jun 29, 2007 (gmt 0)

10+ Year Member



Understood. This solution came out of my lack of knowledge in javascript and especially the AJAX side of things.

I spent a good deal of time searching for a solution and found a lot of people asking but no other solutions. Do you have an example you could provide or a quick AJAX tutorial?

Thanks.

bear

8:42 pm on Jun 29, 2007 (gmt 0)

10+ Year Member



Hi,

I have been working on a similar external link counter, but mine acts between the click and the redirect - i.e. the php code counts the click and then sends the user to the external page.

Ultimately its hard t prevent cheating => in my case the user would have to go back and re-click the link. There's a lot of complex solutions
BUT

What I was thinking of doing was to to add another two fields to the database

user id - computer id of the user
time - when last click to the url was made

each time the link was clicked it would check the time and user id for that URL. You could then put in a test to not update the counter if the latest user id was the same and the click occurred with say 10 seconds of the previous user.

sorry don't have the code its just an idea.

Cheers

Fotiman

3:07 pm on Jul 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I would suggest looking into something like the Yahoo UI Library's Connection Manager [developer.yahoo.com]. You might also check out some of Dustin Diaz's screencasts... one of them demonstrates how to create a contact form using AJAX in less than 15 minutes (you might find the AJAX portion of that useful).

Bernard Marx

11:39 pm on Jul 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On times gone by I've helped a couple of people with a low tec method that seemed to work for them. Simply create an image and add a query string to the src.