$USER->id sometimes doesn't work

$USER->id sometimes doesn't work

by Dietmar Malli -
Number of replies: 10

Hy,

 

I'm, quite new  to moodle, and it's functions. I need to write the Userid of the person calling my script to get written in a .txt file. My script works so far, but only sometimes. Sometimes $USER is just an empty array with one variable called id which is 0, and sometimes it works perfectly.

$configphp is the path to the config.php and require is working... it returns 1.

Here is my script:

 

require($configphp);
global $USER;
//require works this way smile

//Userid:
$info = $USER->id; //and thats the point --> sometimes just returns 0 with every logged in user, somtimes works like a charm

//filehandle
$dokument = "./Personen.txt";
$filehandle = fopen ($dokument, "a");

//Zeilenumbruch erleichtert späteres einlesen in Array
$text = "$info\n";

//Schreiben
fwrite ($filehandle,$text);

//filehandle schließen
fclose ($filehandle);

 

Hope you can help me. Do i have to require something else too?

 

Greetings and Thanx,

Didi smile

Average of ratings: -
In reply to Dietmar Malli

Re: $USER->id sometimes doesn't work

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I'm not sure if this is what's causing the problem, but you shouldn't need the "global $USER;" line unless you're inside a function. Requiring config.php should give you access to $USER automatically.

In reply to Mark Johnson

Re: $USER->id sometimes doesn't work

by Dietmar Malli -

I've tested both... with and without this line... doesn't matter... sometimes it works, sometimes not.... Is there any other way to get the ID? To query the db manually, or get the id somehow out of the session cookie?... Any way? Cause I can not count on this function if its rarely working :/

 

Gretts and Thanx Didi smile

In reply to Dietmar Malli

Re: $USER->id sometimes doesn't work

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

To ensure $USER is set up, you should call require_login.

Well, you should call require login to ensure your script is secure. As a side-effect, that will ensure $USER is set up properly.

In reply to Tim Hunt

Re: $USER->id sometimes doesn't work

by Dietmar Malli -

require_login(); just redirects me to login again, even if I am already loggen in?

 

 

Greetings and Thanx Didi

In reply to Tim Hunt

Re: $USER->id sometimes doesn't work

by Alejandro Michavila Pallarés -

Hi Dietmar,

$USER is an object that keeps the current logged user record, so $USER->id changes depending on who is logged in.

You can pass the course as argument to require_login, for example: require_login($course, true, $cm);

In reply to Alejandro Michavila Pallarés

Re: $USER->id sometimes doesn't work

by Dietmar Malli -

Yep, but how should that fix my problem, of $USER working only sometimes?

In reply to Dietmar Malli

Re: $USER->id sometimes doesn't work

by Alejandro Michavila Pallarés -

Try adding this line to your script:

print_object($USER);

And see what happens.

Also increase debug level to developer.

In reply to Alejandro Michavila Pallarés

Re: $USER->id sometimes doesn't work

by Dietmar Malli -

Yes... I know this command. Normally it displays all Variables in $USER.......
Sometimes this works and if not, then it just displays only the $USER->id Variable as 0. No further Variables.... :/

 

 

Greets and Thanks

Dietmar Malli