• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 409
  • Last Modified:

count unique clicks

i have a site with links, i want to count unique clicks, i use cookies like

"linkcookie.$var"

is this the best way?
0
svsvsv4
Asked:
svsvsv4
  • 2
  • 2
  • 2
  • +3
3 Solutions
 
minichickenCommented:
Do you mean counting unique clicks of the link of the whole site,
And see how many users has clicked that particular link?

So basically, counting unique visitors of a particular page?
0
 
svsvsv4Author Commented:
no, i need to have restriction of times user clicks, i mean, that if someone clicks, the cookie will count only 1 time,
if he clicks again, it won't count....
0
 
hernst42Commented:
There is no way to achieve this without a proper user-authentification
If you want to count unique-clicks it only can be made on the server-side. As you know the user you can log the clicks to a database and if that click is in the database it will not count.

If anyone thinks there is a method that will work without user-authentification, may show me the example and I show him why he is wrong.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
svsvsv4Author Commented:
yes, it's the best way, but i have about 400 links, and average user hits about 20-50 each time, i use as i said a cookie:

cookie.$id

which creates a cookie with name like "cookie.300"
so i check it later, by id of the link,
i noticed that some times it's not the best, maybe because the cokies are disabled with that user?
0
 
minichickenCommented:
I think hernst42 is right, cos I've been thinking on how to achieve that and don't see to have a solution for it. Hm....

I think what you need is to have the user login to the site, so the server knows that the user is logged in in order to count the unique clicks. You might also need to have a DB table to keep track of what the users has clicked and what he hasnt clicked. The advantage of storing information in the DB is that, it permanent and unlikely to be interfered, unlike cookies and sessions.

If you do create a DB for this unique count, you will need to use include file in every page to check if the user has clicked through this link before and update accordingly....

regards-
0
 
Diablo84Commented:
The only reliable way is certainly using user authentication. There are various other methods that can be used which do not work to the same degree of accuracy but it depends on how crucial the statistical logging is. Cookies are one of those methods and theres two steps i can think of to try and enhance the method.

The basic step is this, you would have an include file - uniqcount.php (for example) which will be included in every page of your site - include('uniqcount.php'); - Its contents would resemble the following:

<?php
if (!isset($_COOKIE['uniqcount'])) {
 //add one to the count
 setcookie("uniqcount","counted",time()+31556926);
}
?>

So basically if the cookie has not been set then add one to the count (not sure if you are storing this in a database or text file etc so just left it as a comment) then set the cookie. Next time around the cookie will already be set so it wont be counted again.

This could be improved by also storing the users ip in the database and running an additional check before adding one to the count to make sure there ip is not already in the database, eg.

<?php
//mysql connect + dbselect

if (!isset($_COOKIE['uniqcount'])) {
 $check = mysql_query("SELECT * FROM tablename WHERE user_ip_field_name = '".$_SERVER['REMOTE_ADDR']."' LIMIT 1");
 if (mysql_num_rows($check) == 0) {
  //add one to the count
  mysql_query("INSERT INTO tablename (user_ip_field_name) VALUES ('".$_SERVER['REMOTE_ADDR']."')";
 }
 setcookie("uniqcount","counted",time()+31556926);
}
?>

So basically if the cookie is not set it will query the database to see if their IP has already been recorded, if not it will add their ip to the database and increment the count by 1 and will finally set the cookie.

Now there are obvious problems with all of this, namely: the cookie getting deleted, the cookie being rejected by the browser when it is first set, the users IP being dynamic (and hence changing between visits) etc etc.

So the bottom line is, as has been said, the only reliable way is user authentication but where thats not possible make the most of whatever you can to try and render the best results.

|)iablo
0
 
Diablo84Commented:
Also if you was tracking induvidual unique clicks for induvidual links you would of course have to go one step further, prehaps storing page id's in the cookie - and only adding to this series of id's if it is not already present (using a combination of explode, implde and in_array checks). You would then have to reference this against a database table to track specifically what links where clicked but, its not going to be a pretty method when handling users that are just casual browsers and are not logged in.
0
 
jdpipeCommented:
Usually the thing you care about is how many requests have been recorded at the server for the page in question. Because that is the only thing that ensures that you are counting REAL communication.

However it is possible to modify the values of cookies via client-side JavaScript so if you're writing a game etc, and client-side clicks need to be counted regardless of serverside communication, then you could set the cookie to increment in value each time a link is clicked, and then whenever server communication occurs that cookie value would be sent up to the server.

JP
0
 
physcoCommented:
Why don't you just save the IP address using $_SERVER["remote_addr"] along with datetime. Once this is saved you can determine when they clicked it last? This would be pretty reliable except for dial-up users, which would have to disconnect and reconnect to be able to bypass the system?
0
 
hernst42Commented:
physco
Using IP is a very bad thing as a lot of users (e.g. AOL) are passed through a proxy and will have the same IP. THis also counts for employees of large firms that also have to use a proxy.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now