Pregunta

Where should I store data of user that is not yet registered?

Data that I want to store:

  • Search history (to show appropriate products on a store page)
  • Theme preference (dark / light theme)

Cookies? In database by IP of the user? Something else?

I tried for my use cases of using "database by IP of the user" but found nothing useful. Cookie tutorials mostly show common use cases, but it doesn't tell me whether it's actually the best way as of now.

¿Fue útil?

Solución

If it's a small amount of information, you can store it in cookies on the client's device. If it's more data, you might try web storage or even a database on the client.

There is also the option of storing it on the server in whatever format, most likely inside a DB, but don't store it based on the IP. IPs can change and multiple users can share the same IP. Use an identification cookie, similar to a session cookie, to identify the client and their past history. The disadvantage with this approach is that if the client never comes back, you just end up with the data in your database which you will have to delete at some point, with maybe a job of some sort that each month or something, runs and deletes old, never accesed again information. You don't have this disadvantage if you choose to store the data on the client instead.

Licenciado bajo: CC-BY-SA con atribución
scroll top