To show welcome message in Drupal we can identify whether user is logged in or not using simple code snippet below. You can write this code in either block or custom module which adds block or create a variable in template.php to use in a template:
<?php
// 1st snippet
user_is_logged_in() ? t('My account') : t('User account');
// 2nd snippet
if (user_is_logged_in()) {
global $user;
print t('Welcome @name', array('@name' => $user->name));
}
else {
print 'Welcome Anonymous';
}
// 3rd snippet
global $user;
if (!empty($user->uid)) {
print t('Welcome @name', array('@name' => $user->name));
}
else {
print 'Welcome Anonymous';
}
?>
The user_is_logged_in() function checks if user is logged in or not and return TRUE/FALSE.
$user holds data representing current user on site.
// 1st snippet
user_is_logged_in() ? t('My account') : t('User account');
// 2nd snippet
if (user_is_logged_in()) {
global $user;
print t('Welcome @name', array('@name' => $user->name));
}
else {
print 'Welcome Anonymous';
}
// 3rd snippet
global $user;
if (!empty($user->uid)) {
print t('Welcome @name', array('@name' => $user->name));
}
else {
print 'Welcome Anonymous';
}
?>