Hello. You need to write a module "add product to favorites."

The user went to the site, chose the product, clicked add to favorites, the product was added to the MySQL table. But how can you identify the user if he is not registered and not recorded in any cookies?

  • Cookies need to be recorded, obviously) - andreymal
  • Unregistered users, let them keep their favorites (in some localstorage) - vp_arth
  • Π½Π΅ записан Π½ΠΈ Π² ΠΊΠ°ΠΊΠΈΡ… ΠΊΡƒΠΊΠΈ And what caused the failure of the cookie? - ilyaplot

2 answers 2

But how can you identify the user if he is not registered and not recorded in any cookies?

Record it, of course. After the user is identified by a cookie, you can add his ID along with the product id to the MySQL table.

Sessions are not very well suited for this task, due to the obviously limited lifetime.

  • What will be the identifier of a specific user if not the session ID and it is not registered? - Anton Zikov
  • Universally unique identifier - Ipatiev
  • @AntonZikov arbitrary string, obviously - andmalmal
  • @andreymal, there may be problems with uniqueness. If I correctly understood the essence of the UUID, then probably it really suits better. - Anton Zikov
  • @AntonZikov both are arbitrary strings) UUID is also possible - andreymal

Use sessions. Even unregistered users have a session identifier, which can also be saved in the database if necessary and used as a key.

  • Sessions use cookies, and the question says "not recorded in any cookies" - andreymal
  • Sessions use cookies only to store the identifier. If cookies are disabled, the identifier will automatically be added to the URL. And all this is done automatically without the participation of third-party logic. - Anton Zikov
  • Oh, in 2017 so still doing?) - andreymal
  • And here enlighten me, how pkhp will determine that cookies are disabled? - andreymal
  • one
    @andreymal And what, in 2017 came up with an alternative to sessions and cookies? I do not think that this mechanism will change at all in the next 10 years. - ilyaplot