Solved

count unique clicks

Posted on 2004-10-15
12
364 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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
 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

821 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