User Options - DB or Cookies?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

User Options - DB or Cookies?

Post by jraede »

A client has asked me to store user options that remain set between accesses. For example, if a tab on the sidebar is open, he wants it to stay open the next time the user comes to the site. What's the most efficient way of doing this, storing it with cookies or in the database? I'm guessing cookies, but I just want to make sure. Thanks.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: User Options - DB or Cookies?

Post by alex.barylski »

1. Database requires user to login to restore state.
2. Cookies are a decent bet for storing application GUI state

Cookies eventually expire, database is perpetual. Cookies are specific to a machine, which can be benfitial and a hinderence.

The choice is complicated and depends on you and your clients needs, etc.

Cheers,
Alex
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: User Options - DB or Cookies?

Post by Jenk »

Cookie to store user/session identifier and nothing more, all else in the DB.
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: User Options - DB or Cookies?

Post by jraede »

Would there be any noticeable difference in performance if, say, every time a user closes a tab, I run an AJAX update of the database to reflect this option, rather than if I just set/update a cookie?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: User Options - DB or Cookies?

Post by John Cartwright »

jraede wrote:Would there be any noticeable difference in performance if, say, every time a user closes a tab, I run an AJAX update of the database to reflect this option, rather than if I just set/update a cookie?
Most likely no. However, it is highly dependent on your server hardware, server software/configuration, and available resources.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: User Options - DB or Cookies?

Post by alex.barylski »

Cookie to store user/session identifier and nothing more, all else in the DB.
I would personally, rather store GUI positions, dimensions, etc in a cookie, strictly for the speed. Everytime someone resizes a listview column header, resizes a window, repositions a object, etc. It's just easier to stuff that in a cookie, not to mention there is no backend code required to make this work, so the same code can be applied on a application built in PHP or ASP or JSP, etc.

Other than that, I would almost always side with sessions. :)

Cheers,
Alex
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: User Options - DB or Cookies?

Post by Jenk »

Depends on how persistent you want something to be. If you were to want those GUI options to be (more) permanently persisted, then it would have to be server side, and if you wanted those settings to be offered to the user no matter which user agent/terminal they login from, then it would also have to be server side. :)
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: User Options - DB or Cookies?

Post by jraede »

Cookies are more appealing at the moment because there's no back-end scripting involved. I don't think a user would get mad if a tab is minimized when he logs in on a different computer. But I'm interested to see how much an application would get weighed down with a big number of these options and an AJAX call to update the database with each click. With a small number, I don't think there would be much of a difference.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: User Options - DB or Cookies?

Post by Jenk »

I'd be more worried about the stability/persistence of cookies before worrying about server load. :)

Besides which, cookies are sent across the wire with every request, and are processed by your web-server with every request. $_COOKIE is populated every request, etc. Just because a value is in a cookie, does not mean it is not sent over the wire.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: User Options - DB or Cookies?

Post by cpetercarter »

The big advantage of using a database is security. However, there isn't - I think - a security issue here. You simply want to retrieve information about open tabs etc. So go with cookies. You can set them in Javascript if you like eg add to the bits of JS which open/close your tabs. Easy.
Post Reply