PHP: Storing ‘objects’ inside the $_SESSION

I just figured out that I can actually store objects in the $_SESSION and I find it quite cool because when I jump to another page I still have my object. Now before I start using this approach I would like to find out if it is really such a good idea or if there are potential pitfalls involved.

I know that if I had a single point of entry I wouldn’t need to do that but I’m not there yet so I don’t have a single point of entry and I would really like to keep my object because I don’t lose my state like that. (Now I’ve also read that I should program stateless sites but I don’t understand that concept yet.)

So in short: Is it ok to store objects in the session, are there any problems with it?


Edit:

Temporary summary: By now I understand that it is probably better to recreate the object even if it involves querying the database again.

Further answers could maybe elaborate on that aspect a bit more!

8 Answers
8

Leave a Comment