Page 1 of 1

how to better retrieve user object

Posted: Sat Feb 17, 2007 4:56 pm
by jonwondering
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.

Posted: Sat Feb 17, 2007 6:38 pm
by Kieran Huggins
both are fine - I usually cache it in $_SESSION, but you can retrieve it on page load as well, your database won't die under the extra load.

Alternatively, you could cache it on the filesystem and include it on every page, re-writing the cache's copy on an update.

Posted: Sat Feb 17, 2007 6:48 pm
by jonwondering
so selecting a single row from mysql every time a page loads should not mess up mysql too much, right?

Posted: Sat Feb 17, 2007 7:04 pm
by Kieran Huggins
no - IMO people usually spend far too much time saving a few MySQL queries here and there. They're pretty cheap these days!

Posted: Sat Feb 17, 2007 9:00 pm
by Christopher
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.

Posted: Sun Feb 18, 2007 6:22 am
by jmut
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.
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.
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?

Posted: Sun Feb 18, 2007 6:30 am
by dude81
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

Posted: Sun Feb 18, 2007 10:22 am
by jmut
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
Question is not where session is stored, but rather what you store in there and how you make benefit from it. Are you making ..?

Code: Select all

$user = new User('userid');
$_SESSION['user']  = $user;
And then ...get user object from there, alter on update only, so you don't instantiate connections ...and select all data about user per each request.