how to get uid of current view user account/profile

Events happening in the community are now at Drupal community events on www.drupal.org.
Posted by mayur.pimple on June 30, 2012 at 5:27am

Hi

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

Thanks Mayur

Comments

Simple

Posted by rahulbile on June 30, 2012 at 5:33am

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

blank value

Posted by mayur.pimple on June 30, 2012 at 5:56am

_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

Posted by digodrupal on June 30, 2012 at 8:20am

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

Posted by mayur.pimple on June 30, 2012 at 9:03am

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

Don't use global $user

Posted by legolasbo on February 1, 2015 at 12:23pm

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

Posted by neerajskydiver on August 20, 2013 at 6:47am

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

Posted by xeeshangulzar on May 23, 2014 at 6:43am

+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

Posted by ozin on September 8, 2014 at 11:18am

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:

AltStyle によって変換されたページ (->オリジナル) /