Solved

count unique clicks

Posted on 2004-10-15
12
355 Views
Last Modified: 2012-06-22
i have a site with links, i want to count unique clicks, i use cookies like

"linkcookie.$var"

is this the best way?
0
Comment
Question by:svsvsv4
  • 2
  • 2
  • 2
  • +3
12 Comments
 
LVL 12

Expert Comment

by:minichicken
ID: 12317055
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
 

Author Comment

by:svsvsv4
ID: 12317066
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
 
LVL 48

Accepted Solution

by:
hernst42 earned 168 total points
ID: 12317114
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
 

Author Comment

by:svsvsv4
ID: 12317145
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
 
LVL 12

Assisted Solution

by:minichicken
minichicken earned 166 total points
ID: 12317156
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 27

Assisted Solution

by:Diablo84
Diablo84 earned 166 total points
ID: 12317303
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12317318
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
 
LVL 7

Expert Comment

by:jdpipe
ID: 12317680
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
 

Expert Comment

by:physco
ID: 12325572
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
 
LVL 48

Expert Comment

by:hernst42
ID: 12326574
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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now