how to get uid of current view user account/profile

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
mayur.pimple's picture

Hi

I have use hook_menu_alter()
I want to access uid of current view user in menu alter function.

Thanks Mayur

Comments

Simple

rahulbile's picture

hey you can get the viewing user id :
$user_id = arg(1);

blank value

mayur.pimple's picture

_menu_alter(&$items) {
$user_id = arg(1);
dsm($user_id);
}

value is not print. but when i use this arg(1) in hook_user() then print the value.

Try this

digodrupal's picture

global $user;
$user_id=$user->uid; //to get the user id
$user_name=$user->name; //to get the user name

this is current user info

mayur.pimple's picture

i don't want current login user info. i want current view of user page info

Don't use global $user

legolasbo's picture

You shouldn't use global $user because it could expose you to a privilege escalations if you make an error while handling the global user object. A safe alternative is user_uid_optional_load() which will return the current logged-in user if called without an argument.

$user = user_uid_optional_load();
$user_id=$user->uid; //to get the user id
$user_name=$user->name; //to get the user name

getting userid from URL

neerajskydiver's picture

This worked for me

page URL: http://www.example.com/user/

In .module page

$userpageload = user_load(arg(1));
$useridofcurrentuserview = $userpageload->uid;

Neeraj
Valuebound, Drupal development

Subscribing

xeeshangulzar's picture

+1

I were also facing the same issue.

Issue:
I want to get the user id of authenticate user while sub admin is viewing authenticate user profile.

We have a profile item in menu whose path is set as 'user'. So the url is https:www/sitename.com/user instead of http://www.sitename.com/user/[uid].

Solution:
I used menu tokens module and change my menu item path 'user' to 'user/[current-user:uid]'.

It may help anyone.
Thanks

menu_get_object

ozin's picture

I'm using menu_get_object('user') and get user object of view user profile page

Menu

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week