JasonDavis
JasonDavis

Reputation: 48983

How to save article ID's a user LIKES in Cookies?

Let's say you have a blog and a user can "Like" and article. On your average site with a userbase your user would be logged in and this data would likely be stored in a Database but with something like a Wordpress blog where your user is likely not logged into an account, to keep track if they "Like" an article or not you likely store it's id into a Cookie for them. At least that's how most existing solutions do it.

My issue with this is all the solutions I have seen so far create a new cookie for each article a user likes. If you have a large blog with hundreds or thousands of articles and say a user likes 150 articles, that's 150 cookies being sent with each request. Just the thought of this scares me.

My idea is to store each Article ID into the same cookie in eaither JSON format or possibbly just the ID's separated with a |

I would then have to extract all the ID's into an array or something so it adds slightly more processing but would cut down on the HTTP requests

I would love to hear other thoughts on how to do this?

Upvotes: 0

Views: 464

Answers (3)

Christian Mann
Christian Mann

Reputation: 8125

Does the server need to know about it? What if you stored it purely client-side, with localStorage?

Upvotes: 2

MrCode
MrCode

Reputation: 64536

I suggest storing the likes in the PHP session rather than cookies. Using the session you won't run into problems relating to too many cookies. Also, you can't trust cookie data because it may have been tampered with, you need to verify it every time you access it.

Upvotes: 0

CKKiller
CKKiller

Reputation: 1422

unless you do JSON.stringify() you'll need to separate them with a | then explode it into an array. and use another separator if you need to store more than one piece of data for each 'liked' article, extracting the data into a multidimensional array which you can process with a loop or two.

Upvotes: 0

Related Questions