Storing data into session and storing to database upon "major" action Storing data into session and storing to database upon "major" action mysql mysql

Storing data into session and storing to database upon "major" action


If any of these don't apply to your app, then please ignore. In general, I'm against using sessions as caches (especially if anything in the session is going to be written back to the DB). Here's why.

  • Editing the session requires a request from the user. Editing a php session outside of the request-response cycle is very difficult. So if a user Alice makes a change which affects Bob, you have no way to dirty Bob's cache
  • You can't assume users will log out. They may just leave so you have to deal with saving info if the session times out. Again, this is difficult outside of the request-response cycle and you can't exactly leave session files lying around forever until the user comes back (php will gc them by default)
  • If the user requires authentication, you're storing private information in the session. Some users may not be happy about that. More importantly, a hacker could imploy that private information to conduct a social engineering attack against the end-user.
  • Mallory (a hacker) might not be able to use the information you put in the session, but she can poison it (ie. cache poisoning), thereby causing all sorts of problems when you write your cache to your permanent storage. Sessions are easier to poison then something like redis or memcache.

TL;DR Lots of considerations when using a session cache. My recommendation is redis/memcache.


You can also go for in HTML5, check The Guide and THE PAST, PRESENT & FUTURE OF LOCAL STORAGE FOR WEB APPLICATIONS

Local Storage in HTML5 actually uses your browsers database that works as cookies but it stores data permanently to your browser

  1. unless someone by force remove the data from the browser finding the data files
  2. Or if someone remove/uninstall browser completely,
  3. or if someone uses the application in private/incognito mode of the browser,

What you need to do

  1. Copy the schema for required tables and for required columns and update data at a regular interval
  2. you dont have to worry about user's state, you only have to update the complete data from the localStorage to mysql Server (and from the mysql server to localStorage if required) every time user backs to your application and keep updating the data at regular interval

Now this is turning out to be more of localStorage but I think this is one of the best solution available for me.


redis is a good solution if it is available for you (sometimes developers can't install external modules for some reason) what I would do is either go with your Session approach but with encoded/encrypted and serialized data. Or, which I really prefer is to use HTML5 data properties such as:

<someElement id="someId" data-x="HiX" data-y="Hi-Y" />

which BTW works fine with all browsers even with IE6 but with some tweaks, specially if your application uses jquery and ajax. this would really be handful.