Problematic
Problematic

Reputation: 17678

storing data on an "anonymous" user

I'm currently working on developing a Symfony2 app that will not only accept user registrations, but will allow visitors to go through almost the entire flow of the site without creating an account or logging in. Design ideas look something like this (suggestions/improvements welcome):

  1. When a user logs in to their account, data will be persisted to the user/related entities as normal
  2. When an anonymous user hits the site for the first time, an "anonymous user entity" is created for them as if they'd registered, but with something like USER_<session_id> as an identifier instead of a personalized username. Any activity they perform on the site is persisted to this anonymous user entity
  3. When an anonymous user chooses to register, their anonymous user entity is upgraded to a registered user entity, preserving their data for future use
  4. If an anonymous user leaves the site without registering, the anonymous user entity should be cleared after a while to prevent buildup of dead data

What's the best way to go about this? Specifically, what is considered "best practice" for creating/manipulating a User entity for an anonymous user without having to place code into every controller?

Upvotes: 7

Views: 601

Answers (2)

emilecantin
emilecantin

Reputation: 396

I would advise against using the IP address for this, as it could cause problems for users behind a NAT. Using a custom cookie, or the sessionId (PHPSESSID) cookie as an identifier for tracking purposes would be a better idea. Google uses this strategy for its ads business. Stand on the shoulders of giants!

Upvotes: 5

Derrick
Derrick

Reputation: 131

I have something similar to this that I've had to do. What I did was collected the anonymous users ip address (using ($_SERVER['REMOTE_ADDR'])). I then used the ip address for tracking purposes. You can then use that when they register to append their past usage to their newly created account.

You can then just run a simple query to drop any ip address that hasn't had activity in a while (most users have dymanic ip addresses so it will change every so often anyways).

Upvotes: 0

Related Questions