Page 1 of 1

User Options - DB or Cookies?

Posted: Tue Mar 16, 2010 11:10 am
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.

Re: User Options - DB or Cookies?

Posted: Tue Mar 16, 2010 11:26 am
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

Re: User Options - DB or Cookies?

Posted: Tue Mar 16, 2010 12:02 pm
by Jenk
Cookie to store user/session identifier and nothing more, all else in the DB.

Re: User Options - DB or Cookies?

Posted: Tue Mar 16, 2010 3:31 pm
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?

Re: User Options - DB or Cookies?

Posted: Tue Mar 16, 2010 3:34 pm
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.

Re: User Options - DB or Cookies?

Posted: Tue Mar 16, 2010 7:37 pm
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

Re: User Options - DB or Cookies?

Posted: Wed Mar 17, 2010 6:29 pm
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. :)

Re: User Options - DB or Cookies?

Posted: Wed Mar 17, 2010 7:33 pm
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.

Re: User Options - DB or Cookies?

Posted: Thu Mar 18, 2010 12:42 pm
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.

Re: User Options - DB or Cookies?

Posted: Fri Mar 26, 2010 2:06 am
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.