Tony
Tony

Reputation: 287

Is it a good practice to save an object in a session?

I'm developing a generic app on PHP that is dynamically customized for many clients.

I have the class client, where I load the style and preferences for the client.

My question is:

Is it a good practice to save that object in a session? (I think that is the best solution instead of creating the object in each page, that will need lots of mysql querys.

Upvotes: 3

Views: 1713

Answers (3)

Objectoop Oop
Objectoop Oop

Reputation: 61

I think the session will die when client close the browser. If you store it in cookie (Loaded client) ? it not good for security.

When you store mini data in session (Loaded server).that mean you are using some memory at server. What happen at the time you have many client ?

So, the database connection should be destroy at the end of process for each page. Don't worry to use it.

Upvotes: 0

gaurang171
gaurang171

Reputation: 9080

There are few things you need to consider while you deal with session.

  1. You should not store vary large values in session. i think this is not a problem in your case as preferences are usally small enough.

  2. When you store object in session. you might not get realtime updates. as for example lets say same user is logged in using two separate browsers and/or machines and modify preferences in one. in this case other one will not have the updated customization. its not a big problem but depends on your requirements.

I don't see any other problem here. in fact its valid and good solution to store small values in session and avoid db queries.

Upvotes: 5

jay
jay

Reputation: 916

if it's something that won't change, and will just result in constantly calling MySQL queries over and over then yes, that is a good idea.

MySQL queries (and functions in general) are memory/cpu intensive and can affect page load speeds for the PHP, so the less work you are causing it to do the better.

if the returned values are going to be changing constantly it would be better not to, but store what values you can in the session.

Upvotes: 1

Related Questions