How can I retrieve the username of the current user viewing a page, either using php or html..?

Comments

Guddie’s picture

Hi,

You can print the username as follows:
print_r($user->name);

I guess this will help.

TapocoL’s picture

You will need to initialize $user in instances.

global $user;
print_r($user->name);

-Craig Jackson
-Web Developer

dzedward’s picture

perfect, thank you both very much.

v8powerage’s picture

Excuse me for 'hijacking' post, but I have similar issue.

As an admin I'm editing nodes, and I want leave my username (and perhaps editing date too), but using tips from above I'm getting username of current user instead. Any thoughts?

nourheine’s picture

thanks for the answer that works for me, i did add this into a block .Now i want to show this block only for authenticated users , and for anonymous user another block inviting them to login.how to do that plzzz?