Community Documentation

How to insert current logged in user profile information

Last updated March 25, 2011. Created by inzzzomnia on November 16, 2009.
Edited by silverwing. Log in to edit this page.

There are times when you'd like to display information about the current logged in user. It could be a personalized greeting, a contact form where you don't want to make the user fill out fields with information that allready exists, whatever you name it!

Anyways, all information about your registerd users is stored in your database, it's just a matter of getting the information from it and on to your website.
All databases look a bit different depending on what fields you made the user fill out when creating an account.
To find out what's available to you, create a new php-code page and paste the following code:

<?php
 
global $user;
 
$clu = user_load(array('uid' => $user->uid));    //clu: Current Logged in User (something i made up, use whatever you whish!)
 
print_r ($clu);
?>

Preview your page, the result should look something like this:

[field_user_retailer] => Company name Inc [field_user_firstname] => Peter [field_user_surname] => Smith [field_user_telephone] => 321-123 123[field_user_titel] => manager[field_user_type] => Array ( [0] => [1] => [2] => [3] => ) [field_user_homenumber] => 321-321 321 [field_user_cellphone] => 123-456 789 [field_user_private_email] => private_smith@somewhere.com

Ok, now we know where to go to access certain information. Use the info above to create somthing like:

<?php
 
global $user;
 
$clu = user_load(array('uid' => $user->uid));    //clu: Current Logged in User (something i made up, use whatever you whish!)
?>

Hello <?php echo $clu->field_user-firstname; ?>! Your private email is: <?php echo $clu->field_user_private_email; ?>.

which would output:

Hello Peter! Your private email is: private_smith@somewhere.com.

To make your coding more smooth, set it up like this:

<?php
 
global $user;
 
$clu = user_load(array('uid' => $user->uid));  //clu = Current Logged in User

 
$firstname = &$clu->field_user_firstname;
 
$surname = &$clu->field_user_surname;
 
$companyname = &$clu->field_user_retailer;
 
$privateemail = &$clu->field_user_private_email;
  ...and
so on...
?>


Hello <?php echo $firstname  ?>! <br> Your private email is: <?php echo $privateemail; ?>.

I'm sure this can be done in a much better way. Anyway, i hope it helps someone.

Comments

template.php

A good idea is to put such logic into your preprocess functions into the template.php

Example:

/**
* Override or insert variables into the page templates.
*
* @param $vars
*   An array of variables to pass to the theme template.
* @param $hook
*   The name of the template being rendered.
*/

function texturestar_preprocess_page(&$vars, $hook) {
  global $user;
  $clu = user_load(array('uid' => $user->uid));    //clu: Current Logged in User (something i made up, use whatever you whish!)
  $vars['username'] = $clu->name;     
}

so you can use the variable $username in your page.tpl.php.

Thank you! this article has

Thank you! this article has been really helpful to me!

Love ur Post

Its what i wanted ! thanx a lot !

About this page

Drupal version
Drupal 6.x
Audience
Designers/themers, Programmers

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here