Share » Forums » Developer » How to know if user is logged in from...

How to know if user is logged in from inside template code

How to know if user is logged in from inside template code

Tuesday 15 July 2008 9:08:39 pm - 2 replies

Modified on Tuesday 15 July 2008 9:10:25 pm by Michael Scofield

Author Message

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

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu