Using cookies with PHP and MySQL is good practice?

I have a site that stores registered user site settings in the MySQL database, and I hope to change this so that unregistered users can use browser cookies to benefit from the site settings.

Is there a general way to do this? My thoughts were to create an additional database table with these fields:

id unique_cookie_hash site_preferences 

If a unique cookie is stored in a cookie, and the site’s settings are a JSON encoded string containing the settings of the guest user. Thus, for a user who is not registered on the site, the cookie will be checked. If a cookie exists, it will try to set the settings using the value stored in the cookie. If the cookie does not exist or no match is found, the site will create it and assign it default values.

When the guest makes a change, the site will attempt to launch an UPDATE request on its unique cookie_hash.

I suppose this will work (and that this is how most sites do it?), But since this is a newer concept for me, I was wondering if anyone knows of any good tutorials or "get-yas" to follow up with this method.

Thanks.

+4
source share
3 answers

This will work, just note that by serializing your parameters into a JSON string, you cannot select anything from them.

So, if you want to know how many registered people, such as color scheme A, for example, you will need to make all the code in PHP (at least not in a bulky way).

+3
source

PHP has its own set of functions for managing sessions, and there are alternative (some may say better) libraries for processing the session. This is a complex problem with unforeseen security considerations, so I would not use a home solution without first studying it.

Assuming that you have learned how to safely store session information, you can save site settings for users, and when a user logs into their account, just copy your saved settings to your session, on which the site will act.

0
source

It depends on the number of settings, but in general I will not understand the database at all, but I save their settings in a cookie.

-3
source

Source: https://habr.com/ru/post/1348031/


All Articles