I’ve read online (on Google’s own recommendations for site optimisation techniques) that using cookies should ideally be kept to a minimum – the less cookies (and the less content they have), the better, since every HTTP request to the page under that domain will also transfer the cookie content along with that request.
Our client has a requirement to allow a visit to “save searches” on the products list page. For example, for shoes, I can filter “black shoes” and save my search. I can then filter the black shoes into “leather” so I get “black leather shoes” filtered results, and I should be able to save that search too.
The saved search item therefore includes 3 attributes: a search name (entered by the visitor), the current URL to save, and extra further descriptive information about that page (taken from the page).
Since a visitor can store unlimited searches, I’m planning to use an array and serialize / base64_encode it so that it becomes a string, which I can decode when it needs to be retrieved / displayed.
If I were to store this in a cookie, and the visitor saved say 5 searches, then that’s a lot of text in the cookie. How would you build this? At the moment I can see 2 options:
1) Stick to the approach above, of using a cookie only. For this would you:
a) Do the above of serialize/base64_encode ?
OR
b) Would you “implode” the multi-dimensional array to a string?
OR
2) Would you store all this info in a database, say store the above serialized string in the database, and then assign a unique ID (from the database) to this customer and store that ID in the cookie. In that way, the cookie is just a number, and we retrieve the rest from the database. Would you classify this as a better form of “optimisation” for site speed?
Many thanks!
Without a doubt, store the data in a database and just put the unique saved ID in the cookie.
As well as speeding up your pages by reducing your HTTP request data, you can store an unlimited amount of data in your database whereas a cookie can only store about 4K worth of data.
You will also get the added benefit of the user not being able to tamper with the cookie data if all there is is an ID.