Jump to content

Archived

This topic is now archived and is closed to further replies.

mshallop

SESSIONS v. sql storage (performance?)

Recommended Posts

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....

--Mike

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.