Problem with isLoggedIn() and version preview

Permalink
I have a quick check built into my template to see if someone is logged in or not by using isLoggedIn() - this seems to cause a problem when using the versions feature and comparing different versions however. In the preview I receive an error
call to a member function isLoggedIn() on a non-object...
. Is there any way to just prevent the isLoggedIn() section of the template from running in the preview?

hursey013
 
andrew replied on at Permalink Reply
andrew
Try calling $u = new User(); right before calling isLoggedIn()
hursey013 replied on at Permalink Reply
hursey013
Thanks. Just to confirm - do I still need the global $u if I'm using $u = newUser();? In other words should it look something like this
global $u;
   $u = new User();
   if ($u -> isLoggedIn()) {
or
$u = new User();
   if ($u -> isLoggedIn()) {
Thanks, I appreciate the help.
ryan replied on at Permalink Best Answer Reply
ryan
Always use:
$u = new User();


That will just grab the current logged in user from the session. It won't even make a database query so there's really very little overhead.

the global $u isn't always a valid user object.
hursey013 replied on at Permalink Reply
hursey013
Perfect, thank you.