10

I want to create a table(postgres) that stores data about what items were viewed by what user. authenticated users are no problem but how can I tell one anonymous user from another anonymous user? This is needed for analysis purposes.

maybe store their IP address as unique ID? How can I do this?

Jahongir Rahmonov
  • 13,083
  • 10
  • 47
  • 91

2 Answers2

8

I think you should use cookies.

When a user that is not authenticated makes a request, look for a cookie named whatever ("nonuserid" in this case). If the cookie is not present it means it's a new user so you should set the cookie with a random id. If it's present you can use the id in it to identificate the anonymous user.

Ionut Hulub
  • 6,180
  • 5
  • 26
  • 55
2

Option 1

Use the IP Address. Checkout this answer. There is no need to write the IP into the session because you could always get the client IP when you receive a request.

Option 2

Generate the unique ID by the uuid, as the doc said. And set the ID in the session with a given name, suppose it USER_ID.

When you get the request from a user, check if the USER_ID in session. If so, read the value of it and write a record to database like user id visit page X. If not, generate and set.

Community
  • 1
  • 1
liushuaikobe
  • 2,152
  • 1
  • 23
  • 26
  • 4
    Using an ip address is a bad idea. Some isps are load balancing traffic using multiple proxies, meaning a single session can have many ip addresses. Second, it is common for many (thousand) clients to share the same ip address. – Dog eat cat world Jun 23 '15 at 15:50