hi everyone,
how would i be able to tell if a person who is viewing a given page (or node) is logged in to drupal? basically i want a page to act differently depending on whether or not the user is logged in, like:

if user_logged_in {
// execute code for logged in users
}
else {
// execute code for non-authenticated users
}

i couldn't find anything in the drupal api to do this with. any tips?

Comments

Chris_Allen’s picture

This should work though I'm not sure this is a "best practice" way to do it - I'm relatively new to drupal.

global $user;

if($user->uid == 0){
print "user not logged in";
}

As I understand it, if the uid=1 then it's the root user, values above 1 are authenticated users.

-Chris

OpenChimp’s picture

You can always find cool snippets of code by searching the handbook.

Here's a related way of doing it.

http://drupal.org/node/64854

orionvortex’s picture

Or you can always use the buddylist module and add all users to your buddylist. Then whenever you check your buddylist you can see who is logged in and who is not.

http://chibabeat.com/