views:

1148

answers:

6

Hello,

I am wondering whats more efficient, to store temporary data (related to that session) in a session using say $_SESSION variable in PHP or store and retrieve from an SQL database?

Thank you for your time.

+1  A: 

What's more efficient will depend on the amount of data you want to store and what do you plan to do with the temporary data. I've sometimes stored 5 megs in session data in file storage and it was an awful performance killer. But 5 megs of state is an awful lot and you really shouldn't get there.

Anyhow you can configure PHP's sessions to be stored in a database table and get the best of both worlds.

Still, if the data is not properly characteristic of a user session, then you should not use sessions and use some model object instead.

Vinko Vrsalovic
A: 

Using sessions are better.

Rithish
A: 

Can't talk about PHP-sessions (I have never worked with PHP), but on Asp.net they (Sessions) are just wonderful and easy to work with + LESS DB calls.

A: 

PHP sessions are faster than DB access. But PHP sessions have some known issues.

You may want to look at memcached if you want really fast access times, while avoiding the pitfalls of PHP session management at the same time.

Cheers,

jrh.

Here Be Wolves
+5  A: 

Keep in mind the session variable is backed by a storage mechanism, that is, when the request finishes the session gets written by the session handler, by default this is to a file. On the next request it is pulled back from that file (or whatever else the session handler uses).

If you're reading and writing this data on every request, just stick with a the $_SESSION variables, the overhead of connecting, querying and updating a database will not be faster than the default $_SESSION.

You'll probably only ever want to use a database backed session if you are running multiple load-balanced servers and need to share the session data between them. In this case, if you find the overhead of the database sessions to be slowing down your site to a noticeable degree you might consider sticking memcached between your web server and the database.

linead
A: 

It really depends on the volume of data you intend to store and the amount of traffic you intend to handle. If the data is minimal and the site does not need to scale beyond one web server, by all means use the default session handler which writes the session data into the filesystem of the webserver.

If you need to scale beyond one box, it is recommended that you store your session data into either a memory database such as memcached or regular database. You can override the session handler in PHP and write your own implementation to store to database when using $_SESSION.

Shoan