3

This is a very broad question; I'm just looking for the best way to do it.

I want to be able to differentiate a unique user. I need to be able to track when a user visits a specific page.

Here are the problems that I and other people have encountered :

  • Using an IP address is not effective because there might be many computers on an IP address
  • Using COOKIES encounters a problem if the user clears his/her cookies in a given period of time.
  • Using SESSIONS encounters a problem if the session expires.

I am considering a combination of all three, but I'm still pretty sure this will not catch all the exceptions. I need a system similiar to the way Youtube tracks views on their videos.

Does anyone have any other ideas to this? (Aside from implementing a user system)

mlishn
  • 1,689
  • 14
  • 19
  • 1
    What's the use case? I mean, is it for voting in some fairly insignificant poll, or tacking page visits, or something really important? EDIT: Sorry I didn't read: "I need to be able to track when a user visits a specific page." So it's just for monitoring page views or what? If that's the case I'd just go with the cookie, your data will still be *mostly* accurate which is fine, people won't be abusing this on purpose. – Wesley Murch Aug 15 '12 at 15:11
  • True, if it's important then they should probably have a login. In which case, identifying a user is as easy as logging in. – user1477388 Aug 15 '12 at 15:12
  • This won't be possible at all, especially not reliably (as you seem to wish) unless you're getting people to identify themselves (e.g. logging in). – Thomas Clayson Aug 15 '12 at 15:13
  • If a use opens 2 tabs in a same browser, do you want to identify them as different user? – Shiplu Mokaddim Aug 15 '12 at 15:14
  • you can have several options, each with downsides and different hitrates. But are probably some questions about that allready around here. – Nanne Aug 15 '12 at 15:14
  • @shiplu.mokadd.im thats another one I haven't thought of :x no, that is a single user. – mlishn Aug 15 '12 at 15:16
  • @user1477388 I want to be able to track views from people who are not logged in (for example image views, but **UNIQUE**) – mlishn Aug 15 '12 at 15:18
  • If you want to make sure you identify them, then make them register and login with a username and password. – raygo Aug 15 '12 at 15:14
  • @WesleyMurch right, I just want to do a simple tracking for viewing content such as images and videos. A great example is what youtube does to track their video view counts – mlishn Aug 15 '12 at 15:20
  • I know that Google Analytics checks for unique visitors with a cookie set to expire in 2038. I would imagine youtube does something similar – raygo Aug 15 '12 at 15:21

3 Answers3

2

Without using cookies/requiring users to login to identify themselves - determining whether they are "unique" or not will be nearly impossible. There are a few methods to help, but neither are guaranteed:

You can use PHP Sessions - without requiring an actual "user management system".

session_start();
$id = session_id();
  • The upside to this is, you'll be able to track the current user as long as their session remains active.

  • The downside is, you won't be able to determine if the "new user" is unique or not if it's an old user who's session has cleared (much like if the user has cleared their cookies).

An alternative, but also along the same lines as using just the IP address, is to attempt a best-possible fingerprinting algorithm using different variables from the $_SERVER environment info:

$userFingerprint = $_SERVER['REMOTE_ADDR'] . ':' . $_SERVER['HTTP_USER_AGENT'] . ':' . $_SERVER['HTTP_ACCEPT']; // plus any others you might find helpful
  • The upside to this is, there may be enough data to differentiate between users on the same network.

  • The obvious downside is, there may not be enough data to differentiate between users on the same network.

newfurniturey
  • 37,556
  • 9
  • 94
  • 102
1

I think, there isn't any absolute way to do this. Users can change ip address, delete or change cookies etc., because everthing is on their hand.

But according to system (poll, counter) which you are planning, you can develop a way to determine unique user. For example to develop a poll system, you can insert ip, http_user_agent to database and also to increase quality you can set some cookie.

Bilal Gultekin
  • 4,831
  • 3
  • 22
  • 27
1

One other way may be to create a table in your database capturing the following data:

  • IP address
  • Operating system/ browser etc. (any other info that would be specific to that user)

Then, you could narrow it down and say, "A user at this particular IP address using this particular OS on this particular browser downloaded this image at this particular time."

Is that what you want to achieve?

user1477388
  • 20,790
  • 32
  • 144
  • 264
  • I know, but I attempted to resolve that by adding the other fields you could narrow it down. I am sorry, but this may be the only way to determine a specific user if you don't trust the IP address. – user1477388 Aug 15 '12 at 15:37
  • I could use all the `$_SERVER` options but this still wont differentiate the users. Imagine a group of users in a library on a single ip address using the same operating system. – mlishn Aug 15 '12 at 15:40
  • I understand, but I don't know how else you could identify a user without using cookies, sessions, or recording specific information about them such as their $_SERVER data. – user1477388 Aug 15 '12 at 15:41
  • Right, and I have no idea either, thats why I was hoping someone here does. I'm pretty sure the way Youtube does it has minimal room for error, thats the way I'm looking for – mlishn Aug 15 '12 at 15:43
  • Perhaps, youtube does a combination of methods i.e. if the cookie has been deleted, try the next best option? – user1477388 Aug 15 '12 at 15:44
  • If i were to guess, they use all three and relate them to some kind of database table for coherence. For example, if cookie was created on ip address (in table) and has a current session --------------- or --------------- if cookie matches the ip address and no current session, then create session. ---------------- etc.. – mlishn Aug 15 '12 at 15:47
  • This post may be good reading for you http://stackoverflow.com/questions/8968215/how-does-youtube-remember-likes – user1477388 Aug 15 '12 at 15:49
  • yeah, thats simple. If each user has an id, then its easy to keep track of what they do. I'm trying to figure out how to deal with the random viewers (not registered members). This is strictly to keep track of views – mlishn Aug 15 '12 at 15:52