How To Test If a User Is Logged In For Drupal

0
Your rating: None

The correct way of testing if a user is logged in is to test whether $user->uid is 0:

global $user;
if ($user->uid) {
   $output = t('User is logged in!');
else {
   $output = t('User is an anonymous user.');
}

This approach is often used when defining a block that shows one thing to
logged-in users and something else to anonymous users:

<?php
   global $user;
   if ($user->uid) {
     return t('You are currently logged in!');
   }
   else {
     return t('You are not currently logged in.');
   }
?>

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.

Cumulus Tag Cloud

Kiran Says

I love work environment which is:

Informal yet professional
Demanding yet rewarding
Challenging yet inspiring
Mediocrity is not an option
Having fun is serious business
Making mistakes is human
Forgiveness is Company Policy

Travelling Sucks