General developer forum

How to get user id of current user in moodle?

 
Picture of Azmat Ullah
How to get user id of current user in moodle?
 

i want to create php file where i get user id of current moodle user.

How to get user id?

which file will include in php file for getting id?

 
Average of ratings: -
Matt Bury
Re: How to get user id of current user in moodle?
Group Particularly helpful MoodlersGroup Plugin developers

Hi Azmat,

What do you want to do? Create a new activity module? A resource? A block? Until we know something more specific, it's difficult for us to help you.

Typically, you can access the global objects for $CFG, $USER, $COURSE, etc. and get current data from those. It depends where your PHP script is and what it's doing.

All the best,

Matt

 
Average of ratings: -
Picture of Jamie Pratt
Re: How to get user id of current user in moodle?
Group Core developersGroup Documentation writersGroup Particularly helpful MoodlersGroup Plugin developers

The global variable $USER will work everywhere in Moodle. If within a function or a class method and not in the global scope you need to put :

global $USER;

at the beginning of the function.

$USER is all the info from the current users user record and then some more I think.

$USER->id gives the id of the user or there is also $USER->idnumber which is the id number editable in the user profile and sometimes passed from a student information system etc.

 
Average of ratings: Useful (1)
Picture of Rakesh Goudar
Re: How to get user id of current user in moodle?
 
Can we even find out the USER name i.e, can we echo the user name by using $USER->name ?
 
Average of ratings: -
Davo
Re: How to get user id of current user in moodle?
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developers

Take a look in the database as the mdl_user table - you can output any of the fields found in there, from the $USER global (plus a few extra, such as the custom profile fields).

So no, there is no such field as $USER->name, but there is $USER->username, $USER->firstname, $USER->lastname and, if it is what you are looking for, fullname($USER) (to output the configured full name of the user - defaulting to '[firstname] [lastname]' on a fresh install).


 
Average of ratings: Useful (1)
Picture of ranu jaiswal
Re: How to get user id of current user in moodle?
 

Hi,

I want to get the current userid and courseid in javascript. I wrote my javascript in Additional HTML section. How can i get UserID  and course of logged in user here please rpl asap!

 
Average of ratings: -
Picture of Richard Oelmann
Re: How to get user id of current user in moodle?
 
Average of ratings: -
Picture of ranu jaiswal
Re: How to get user id of current user in moodle?
 
I tried but its not working. When i alert the php variable in js it is giving null value. I am using 2nd option of DOM 
 
Average of ratings: -
Picture of electo TheBledard
Re: How to get user id of current user in moodle?
 

No $USER->id doesn't work everywhere.

 
Average of ratings: -
Picture of Richard Oelmann
Re: How to get user id of current user in moodle?
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developersGroup Testers

If the user is logged on then $USER->id should work - but if you are writing your own code, you may have to declare the $USER variable as a global in your code. 

 
Average of ratings: -
Picture of Howard Miller
Re: How to get user id of current user in moodle?
Group Core developersGroup Documentation writersGroup Particularly helpful MoodlersGroup Plugin developers

require "../path/to/config.php";

require_login();

$userid = $USER->id;


I think that covers it.
 
Average of ratings: Useful (4)
Picture of Daniel Garcia Sanchez
Re: How to get user id of current user in moodle?
 

Hi Howard! smile

Thanks very much!! It helped me.

Best regards, Daniel

 
Average of ratings: -
Picture of Alejandra Reinoso
Re: How to get user id of current user in moodle?
 

Hi, I'm not manage php lenguage, could you post all the php source?

 
Average of ratings: -
Ben Ellis - chilled ;)
Re: How to get user id of current user in moodle?
 

Are you sure about what you are trying to do if you do not know PHP? You try this.

<?php
require('path/to/config.php');

global $USER;

$userid = $USER->id;

echo $userid;

?>

 
Average of ratings: -
Picture of T Nguyen
Re: How to get user id of current user in moodle?
 

Hello...  I put the above code in ~/public_html/test1.php and moodle 2.3.2 is in ~/public_html/mymd .  After I logged in to moodle and executed test.php, userid is 0 and I noticed that I was no longer logged in. 

<?php

//test1.php
require('mymd/config.php');
global $USER;
$userid = $USER->id;
echo $userid;
?>

 If I put test2.php in ~/public_html/mymd directory, userid is returned correctly.

<?php

//test2.php
require('config.php');
global $USER;
$userid = $USER->id;
echo $userid;
?>

What did I do wrong in test1.php?  What configuration didn't I do?  Please help.  Thank you.

 
Average of ratings: -
Picture of Matteo Scaramuccia
Re: How to get user id of current user in moodle?
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developers

Hi,
starting from 2.3 the Moodle session cookie path has been restricted to the Moodle installation folder: you need to go to Settings > Site administration > Server > Session Handling and set that path, sessioncookiepath, to "/".

HTH,
Matteo

 
Average of ratings: Useful (1)
Picture of T Nguyen
Re: How to get user id of current user in moodle?
 

Hi Matteo, thank you!

 
Average of ratings: -
Picture of Amin Farajzadeh
Re: How to get user id of current user in moodle?
 
thank youuuuuheart
 
Average of ratings: -
Picture of Azmat Ullah
Re: How to get user id of current user in moodle?
 

Thanks to all guys who replied specially Howard Miller

I have done my task...

 
Average of ratings: -
cctv camera dealers in coimbatore
Re: How to get user id of current user in moodle?
 

same problem here

 
Average of ratings: -
Picture of Marcus Green
Re: How to get user id of current user in moodle?
Group Core developersGroup Particularly helpful MoodlersGroup Plugin developersGroup Testers
You say you have the same problem, but this thread appears to have several detailed explanations of how to solve this problem. Could you give more details of your problem.
 
Average of ratings: -