How do you store 'recently seen' in Cookies?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do you store 'recently seen' in Cookies?

Post by simonmlewis »

Hi
We want to display products a customer has looked at - perhaps just the last 10 or 20.
We did try this via a database, but the storage shot up, and I've seen it on web sites where if you delete cookies, it's gone.

So how do you store up to 10 product ids, in cookies?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: How do you store 'recently seen' in Cookies?

Post by tr0gd0rr »

I would use a database. To keep the table small, periodically delete entries older than a certain date. If you do use cookies, the PHP manual suggests using explode(). You could also use http_build_query()/parse_str() or json_encode()/json_decode().
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you store 'recently seen' in Cookies?

Post by simonmlewis »

Well I have created the db and php scripts to delete old and only had new - so it never goes over 20 entries. IT does work.

But wondered if cookies was a better way of going? The DB Method uses their IP to store them. So you could end up having entries that will never be deleted, unless manually deleting.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: How do you store 'recently seen' in Cookies?

Post by tr0gd0rr »

You could assign them a "logged out user" id and store that in the table instead of IP address. Set that id as a cookie.

With cookies, one drawback is that every HTTP request the browser sends the cookie data to the server and the server sends it back. Even static files like js, css, and images.
Post Reply