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

netentropy’s picture

I am interested in this also

http://www.opensourcefriends.com - Ask the simple questions.

Gman’s picture

Not really an API things, but searching the handbooks, I came across http://drupal.org/node/64854.

The reason is works it that for non-authenticated users, the uid equals 0, which returns as false in the above example.

You can also use:

global $user;
if ($user->uid == 0) {
'code for non-logged in user'
} else {
'code for logged in user'
}

----------
My Drupal/Tech Thoughts
vbDrupal Articles at Skejo.com

pwolanin’s picture

Using PHP input format, you can use PHP tags mixed with normal markup like:

<?php
global $user; 
if (!$user->uid) : ?>

<p>If you are a site member, log in at the right to gain access to member-only content. </p>

<p>Other content for anonymous users here</p>

<?php else: ?>

<p>Thank you for logging in- you now have access to additional menus and content for members only.</p>

<?php endif; ?>

---
Work: BioRAFT

BioALIEN’s picture

This wont work on files outside of Drupal's core. What's the solution to replicate this on a file thats outside of Drupal but is being outputted via Drupal?

I got too lazy and wrote a module in a separate php page and used iframes to get it into Drupal. *shameful i know*

------------------------------
BioALIEN
Buy/Sell/Trade with other webmasters: WebMasterTrader.com

ihsanullahkhan’s picture

You can use this,

<?php if (user_is_logged_in()): ?>

  <p>Hello, logged in user</p>

<?php else:?>

  <p>Hello, stranger</p>

<?php endif;?>