Forums / Developer / Finding out whether a user is logged in or not

Finding out whether a user is logged in or not

Author Message

Eirik Alfstad Johansen

Tuesday 11 November 2003 11:34:13 am

Hi,

[EDIT]: Apparently I was a little bit too quick to post. It hit me that there probably existed an is_logged_in attribute for the current_user, and that was of course correct.

What's the best way to find out (in a template) whether a user is logged in or not? I've read about using "current_user" for the fetch function, but that doesn't give me the anonymous user id to compare with.

Up until now, I've been using the following in my pagelayout.tpl:

{section show=eq($current_user.contentobject_id,$anonymous_user_id)}

Though there's no documentation of the variable $anonymous_user_id being passed to pagelayout.tpl, this works like a charm. For the other templates, however, this variable seems to be unavailable.

So, what is the best way to detect whether a user is logged in or not?

Thanks in advance !

Sincerely,

Eirik Johansen
Netmaking AS

http://www.netmaking.no/

Sincerely,

Eirik Alfstad Johansen
http://www.netmaking.no/

Bruce Morrison

Tuesday 11 November 2003 2:31:13 pm

Hi Eirik

I've used the following on a number of sites...

{let thisuser=fetch('user','current_user')}
{section show=$thisuser.is_logged_in}
<p>Welcome. You are logged in as {$thisuser.contentobject.name}</p>
<p><a href="/user/logout">Logout</a></p>
{section-else}

<!-- login form goes here -->

{/section}
{/let}

Cheers
Bruce

My Blog: http://www.stuffandcontent.com/
Follow me on twitter: http://twitter.com/brucemorrison
Consolidated eZ Publish Feed : http://friendfeed.com/rooms/ez-publish