Jump to content


SESSIONS v. sql storage (performance?)

  • Please log in to reply
2 replies to this topic

#1 mshallop

  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 06 March 2006 - 04:03 PM

Question - in an application of hundreds of php source scripts, does it make more sense to object-ify your session storage via mySQL instead of the _SESSION functions? Granted, some limited data would still have to be session-ized (at the minimum, the user/group id), but if you're storing several tables worth of data in a session, at what point does it become more efficient (if it does) to load everything from a database instead via class functions?

Factor in considerations such as a load-balanced server environment....does this change the equation?

Just curious if anyone else has dealt with this....


#2 wickning1

  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 06 March 2006 - 06:54 PM

Load-balanced servers will definitely make a difference. PHP sessions are stored on disk, so different web servers will not know about each other's sessions.

I don't think there are significant performance differences either way, as long as your data is stored efficiently in both cases. You'll have to make more tcp/ip connections to get the data from mysql, but mysql has data access/caching optimizations that will help neutralize that cost.

#3 txmedic03

  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 07 March 2006 - 09:50 AM

Well, it all depends on your needs, but I handle all of my sessions with mysql. I use session_start() to begin the session with a unique sid then session_id() to reference the row in the sessions table. Every user has a row in this table so I can track current users viewing the site. If the user does not load any pages with in 10 minutes the row is removed from the table. I do this by using time()+600 and comparing it to time(). if time() >= {value retrieved from mysql for time()+600} then expire delete row from table otherwise update row with time()+600 to continue session for 10 minutes from now. Incidently, you do not have to store your username/password or anything in the $_SESSION since the session_id() returns a unique identifier for your sessions. If you would like more detailed information on the way I did the user tracking and handled the sessions so you can benchmark it against storing data in $_SESSION, let me know and I would be happy to share an example script with you.


I can't stop you from doing something silly, but at least I can help you do it right.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users