3

I have a login page. User can login to their account and can get their details. But I am confused, should I store the users details in the session variables or should I pass only the ID as session variables, and in next page all the details retrieved by class using the ID.

If I pass only the ID as session variable, then I have to call the class in every page. Please give me the right idea, I don't want to call the class repeatedly, the page should load faster.

Thanks in advance.

1
  • 1
    You can always store an object as a session. The object contains all of its properties. Commented Feb 7, 2012 at 19:38

2 Answers 2

2

It won't matter performance-wise for a long, long time. You can pick any of the methods.

In my experience, storing the ID in the session, and retrieving the user data when needed in the user class is the more common way to go.

Storing an object containing the user data in serialized form in the session is also possible, but

  • it can cost a lot of memory (because the session data is loaded into the PHP script on every request)

  • You can't rely on the data being fresh (what if the user changed their preferences, or something else happened?)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your time. The problem is suppose the user have 5 pages to visit and in every page, I am getting the information from DB by calling a Class, and suppose at a time I have 500 users online. Just think about the MySql load. I just want my site will run faster and smoother. So any idea..?
0

I think you should only store the session ID in session variables. You will get no performance benefit if you store some information about the user in session becouse you will never no what kind of additional information will you need later and serialization-deserialization takes some time also (specially when you store sessions in database!).

For exampla i'm always load user data from db in every single request becouse i have to check the user for several reason: is he/she locked, is he/she disabled, what is his/her preferred language, when was he/she here last time and so on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.