Hi everyone!

I am using the Front Page Module to customize my front page - one for Guest, and a separate front page for Authenticated users.

I want to display a line saying:

Welcome !

for my authenticated users. How can I achieve this in PHP coding? I don't know the variable to call that will give me the "current logged in user".

I tried
print $user and print $username
it did not work.

Please help. Thanks!

Jess

Comments

true-pal’s picture

.carey’s picture

I found this some time ago on Drupal, but can't remember where exactly:

Welcome <?php print l($user->name,'user/'.$user->uid); ?>

Cheers,
Carey

jessmagz’s picture

Hi,

Thanks for your reply. But the code you have given didn't work. It just displayed the word "Welcome".

I can't figure out why. Thanks.

.carey’s picture

<?php global $user; ?>

Welcome <?php print l($user->name,'user/'.$user->uid); ?> 

<?php ?>
jessmagz’s picture

A big thanks! It worked at last! I spent the whole afternoon searching... huhuhu!
Thanks...
global $user; is a powerful code :-)

Jess

.carey’s picture

You're welcome. Glad I could help.

METZGERR’s picture

whats the "if" if a user is not registered logged in + how to show than a register/login?

.carey’s picture

Maybe this is what you are wanting:

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

Welcome <?php print l($user->name,'user/'.$user->uid); ?>.   <?php print l("Logout","logout"); ?>.

    <?php else : ?>

Welcome Guest. <?php print l("Login","user"); ?>/<?php print l("Register","user/register"); ?>.
          
<?php endif; ?>
METZGERR’s picture

great, thanks a lot!

novapronoia’s picture

Hi,
I want to do almost the same thing
I don't want to show the user id on navigation menu but on top of header, where should I put this code?

CliveCleaves’s picture

EDIT: You shouldn't turn on the php filter module, but that could get it to work.

Otherwise a little module that uses some of the php posted in this thread will work awesomely. The problem we ran into was that this is not satisfactory for multi-lingual sites, but you can simply have a case on lang. Something like this:

 <?php
    global $language;
     switch ($language->language) {
      case 'en':
        print ; //English string or variables
        break;
      case 'fr':
        print ; //French string or variables
        break;
    }
    ?>
ballerjones’s picture

how would I print profile_name? I tried calling $account globally, but that didn't work.

profjk’s picture

subsribing

bethhauck’s picture

<?php profile_load_profile($user) ?>
<?php if ($user->uid) : ?>

Welcome <?php print $user->profile_name; ?>.
         
<?php endif; ?>
mahi.orai’s picture

i want to show user name in place of my account in user menu.
and please tell me that this code where i past or write

scrapbrainuk’s picture

How would I print this on a seperate page?
like if i wanted to create a seperate PHP file that just sets the currently logged in user to a parameter so you can print that parameter throughout the PHP file.

John_B’s picture

See above comment http://drupal.org/node/213633#comment-5761898 and set the block to show only on that page. Or create a page-specific template in the theme for the page where you want it to appear, and add the code there.

I do not understand when you say

so you can print that parameter throughout the PHP file.

You could make a module to do the job, but why would you want to?

Digit Professionals specialising in Drupal, WordPress & CiviCRM support for publishers in non-profit and related sectors

eRmigue’s picture

I would like to add more field, for example: last_name, however I can't display it

mahi.orai’s picture

can any one tell me that this code where past for show user name when user loged in.
global $user;

Welcome print l($user->name,'user/'.$user->uid);

or simply i want to show user name

erok415’s picture

I'd like to sum this up. The best way that I found was to make the following changes.

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

Welcome <?php print l($user->name,'user/'.$user->uid); ?> | <?php print l("Logout","user/logout"); ?>.

    <?php else : ?>

Welcome Guest. <?php print l("Login","user"); ?>/<?php print l("Register","user/register"); ?>.
         
<?php endif; ?>

Notice the user/ before the logout and register. At least with D7, you need to have the "user/" in front of logout, register, etc.

Ain't it great....!

Ilikejava’s picture

It would be really helpful if people could add WHERE the code goes (what file) in their responses for those of us who are new to Drupal. This would really save time and make people more productive sooner.

Thanks for all the great input.

John_B’s picture

This goes in page.tpl.php in the theme. Or in an alternative page template specific to the relevant page (see https://drupal.org/node/1089656). Or in some other template which is printing content, as explained on that linked page.

You have to work out the part of page.tpl.php or other template which is responsible in your theme for outputting the part of the page where you want to add something, and place it in that file.

Digit Professionals specialising in Drupal, WordPress & CiviCRM support for publishers in non-profit and related sectors

lunk rat’s picture

Forget all of the code snippets, you can do this with the Views module! The result is clean, elegant, flexible, and limitless.

Structure > Views > Add new view
Show: Users sorted by Unsorted
Don't create page
Create block: no title
Display format: Unformatted list of Fields
Items per page: blank
Don't use pager

Continue & edit

Change the following:

BLOCK SETTINGS
Block name:None
Access:Role | authenticated user

Advanced (expand on the right)

CONTEXTUAL FILTERS
Add
User: Uid
WHEN THE FILTER VALUE IS NOT AVAILABLE
Provide default value
Type
User ID from logged in user
WHEN THE FILTER VALUE IS AVAILABLE OR A DEFAULT IS PROVIDED
Override title
%1

Edit the field User:Name

REWRITE RESULTS
Rewrite the output of this field
Text: Welcome back, [name].

Save the view

Go to Structure > Blocks > View:[your view] > assign it to a region.

This gives you a block with the default title set as the username of the logged-in user, and shows a message "Welcome back, [username]"

Of course if you know views you know that you can configure the fields to display ANY user-field, like a photo, real name, birthday, last logged in, etc. etc. etc.

Really this is the swiss army knife of all user-specific block needs.

Attached a view export of my example above. Let me know what you think of this!

- lunk_rat

$view = new view();
$view->name = 'welcome_user';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'users';
$view->human_name = 'Welcome user';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'role';
$handler->display->display_options['access']['role'] = array(
  2 => '2',
);
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'none';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Field: User: Name */
$handler->display->display_options['fields']['name']['id'] = 'name';
$handler->display->display_options['fields']['name']['table'] = 'users';
$handler->display->display_options['fields']['name']['field'] = 'name';
$handler->display->display_options['fields']['name']['label'] = '';
$handler->display->display_options['fields']['name']['alter']['alter_text'] = TRUE;
$handler->display->display_options['fields']['name']['alter']['text'] = 'Welcome back, [name].<br />
<em>(<a href="/user/logout" title="Log out">Click here if you\'re not [name]</a>)</em>';
$handler->display->display_options['fields']['name']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['name']['link_to_user'] = FALSE;
/* Contextual filter: User: Uid */
$handler->display->display_options['arguments']['uid']['id'] = 'uid';
$handler->display->display_options['arguments']['uid']['table'] = 'users';
$handler->display->display_options['arguments']['uid']['field'] = 'uid';
$handler->display->display_options['arguments']['uid']['default_action'] = 'default';
$handler->display->display_options['arguments']['uid']['title_enable'] = TRUE;
$handler->display->display_options['arguments']['uid']['title'] = '%1';
$handler->display->display_options['arguments']['uid']['default_argument_type'] = 'current_user';
$handler->display->display_options['arguments']['uid']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['uid']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['uid']['summary_options']['items_per_page'] = '25';
/* Filter criterion: User: Active */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'users';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = '1';
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;

/* Display: Block */
$handler = $view->new_display('block', 'Block', 'block');
stanb’s picture

Works great. Thank you.

Stan B

johnschols’s picture

Hi, I've only been using Drupal for a couple of weeks. Found your post very helpful. Hope all the rest of my setting up tasks go as easy as this. Thanks John

asb’s picture

Nice idea, the View works well in Views 3.x!

If you're on D6 or Pressflow, just change $view->core = 7; to $view->core = 6; in the export, and Views will import it cleanly.

hondaman900’s picture

This post answered my question and saved me a ton of time - thank you! The imported Lunk_rat View was a great starting point that I edited to meet my needs.

Very much appreciated.

charlie1volley’s picture

Huge help, lunk_rat, thanks!