How can I get current user name from PHP script in a module?

I need to perform some user name dependent operations, like creating a directory with user name.

Comments

nevets’s picture

First there are two pieces of information that identify a user, their user id (uid) and user name (name). Of the two, uid id unchanging and is the one I would recommend when constructing information that you need to be able to recreate. If you use user name and they change it, the directory effectly no longer exists since it under the old name. In either case here is how you get the information for the current user.

<?php
  global $user;

  print $user->uid;   // Their user id
  print $user->name;  // Their user name
?>
pyotr777’s picture

Thank you very much! That's just what I needed!

hiweed’s picture

Thank a lot nevets!
I created two fields via the profile module: profile_my_account and profile_my_password.
But I dont know how to get the value of these fields when I create a page with php code?
Thanks in advance!

hiweed’s picture

Hehhh, got it done:

<?php 
  global $user;
  profile_load_profile($user); 
  print "{$user->profile_my_account}"; 
  print "{$user->profile_my_password}"; 
?>
wael_dev’s picture

iam create chat based on drupal system and i want to get the current user id or name which logged in drupal , i try to use it code

  global $user;

  print $user->uid;   // Their user id
  print $user->name;  // Their user name

and no result i use this code in page in chat folder but no result, waht ic an do ??

thx