This question must be asked a lot, but I still couldn't find anything good. I have a lot of php pages that need to use the user object. What I am wondering is what would be a better way to handle this:
1. Make a global user object once logged in, and re-retrieve it only when it is updated, or
2. Create a new local user object for every page if logged in, and go from there.
Obviously the first one will make less queries to the database, but I am still wondering how others handle this. Maybe there's another option. Any ideas?
Thanks.
how to better retrieve user object
Moderator: General Moderators
-
jonwondering
- Forum Commoner
- Posts: 39
- Joined: Mon Mar 13, 2006 6:26 pm
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
-
jonwondering
- Forum Commoner
- Posts: 39
- Joined: Mon Mar 13, 2006 6:26 pm
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
I have thought about that too (storing object in session) and it would make perfect sense. But can someone give us a simple example on how this will actually work. Simple usage and update scenario.arborint wrote:Yeah, I think saving the user object in the session (whether file or DB based) is probably the easiest way. It is probably on of the few example of a where a Singleton makes sense in PHP.
Cause I think there will be resources to be recreated on unserialize (when you get the object from SESSION etc). I guess __wakeup will be used etc.
Is it as complex as I think it is?
As I've seen in the two widely used applications
1) is creating a sessions_table in the database and using it for session_storage (Application: Dotproject)
2)Using Singletonclass you can acheive a constant connection to the database once the user is logged in (Application: Wordpress).
Point 2 has to be applied carefully with lot of considerations. For that you can check the following
Singleton Considerations
1) is creating a sessions_table in the database and using it for session_storage (Application: Dotproject)
2)Using Singletonclass you can acheive a constant connection to the database once the user is logged in (Application: Wordpress).
Point 2 has to be applied carefully with lot of considerations. For that you can check the following
Singleton Considerations
Question is not where session is stored, but rather what you store in there and how you make benefit from it. Are you making ..?dude81 wrote:As I've seen in the two widely used applications
1) is creating a sessions_table in the database and using it for session_storage (Application: Dotproject)
2)Using Singletonclass you can acheive a constant connection to the database once the user is logged in (Application: Wordpress).
Point 2 has to be applied carefully with lot of considerations. For that you can check the following
Singleton Considerations
Code: Select all
$user = new User('userid');
$_SESSION['user'] = $user;