Forums / Developer / How to know if user is logged in from inside template code
Michael Scofield
Tuesday 15 July 2008 9:08:39 pm
Hi all,
Sorry for the simple question, but I've googled a lot and couldn't find how to do it.
Inside a template, what is the best and faster way to know if a user is logged in at the website?
I want to limit some functionallity of my website to logged in users.
In a website made without Exponential we probably could check it using $_SESSION variables. What about with Exponential?
Btw, I found a fetch function that appears to do that:
{if fetch( 'content', 'access', hash( ...) ) }
{/if}
Should I use that or there is a better solution?
Thank youMichael Scofield
Bård Farstad
Tuesday 15 July 2008 11:29:19 pm
You can try this template code:
{if $current_user.is_logged_in} LOGGED IN {else} NOT LOGGED IN{/if}
Also remember to not have cache blocks around this code, or if you do remember to use the user_id part of the cache key.
-bård
Documentation: http://ez.no/doc
André R.
Wednesday 16 July 2008 1:29:41 am
Or if you only need to know if the user is logged in (no need for user id or user name), then use $current_user.is_logged_in as cache key.
Also note that inside node/system templates you'll need to fetch the user first, so use something like this first*:
{if is_unset( $current_user )} {def $current_user = fetch('user', 'current_user') } {/if}
But if you do want to check if a user has access to a certain module / function, then fetch('content', 'access') is a cleaner solution.
* node templates use content view cache, witch is unique pr user rights, but not user id. So you have to disable view cache (search for cache_ttl) if you need to use name / id in a node template.
eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription @: http://twitter.com/andrerom