What I did was actually create a User class. When a user signs in what I did was place the User's UserID into a session variable. Now, within my user class I've created an assortment of static and regular methods to access that user's data.
For instance if you only need the user's username, I'd create a static GetUsername_1( $UserID ) within your user class to query the database and return a the username string. That way, you can just include the class file in your php page and call User::GetUsername_1( $_SESSION['UserID']) each time you need it.
Now, say you need like a bunch of information like the username, first name, last name, and zip code. For this, I actually instantiate the class with an overloaded constructor like
$u = new User( $_SESSION['UserID'] )
Within the constructor you can have a SetUser( $UserID ) method that queries the database again, and sets the different variables that I described above. Then you just have accessor functions like:
GetUsername_0() { return $_username; }
SetUsername( $Username ) { $_username = $Username; }
GetFirstname() //... etc
Then you can write nifty (at least, I think) role functions that can give you things like user roles (or levels) plus certain tests (that could also be static) to figure out what to display on a page.
Something like:
IsAdministrator_1( $UserID )
{
$rVal = false;
if( this->GetUserLevel_1( $UserID ) == "Administrator" )
{
$rVal = true;
}
return $rVal;
}
which you'd then call using:
User::IsAdministrator_1( $_SESSION['UserID'] )
That went on a bit longer than I expected, I sorta got excited 🙂
Disclaimer: I am pretty new to the whole PHP thing, so my ideas might not be the best ones, just something I've thought up to work around my own problems.