$USER->id sometimes doesn't work

$USER->id sometimes doesn't work

av Dietmar Malli -
Antal svar: 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 leende

//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 leende

Genomsnitt av betyg: -
Som svar till Dietmar Malli

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

av Mark Johnson -
Bild på Core developers Bild på Particularly helpful Moodlers Bild på Peer reviewers Bild på 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.

Som svar till Mark Johnson

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

av 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 leende

Som svar till Dietmar Malli

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

av Tim Hunt -
Bild på Core developers Bild på Documentation writers Bild på Particularly helpful Moodlers Bild på Peer reviewers Bild på 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.

Som svar till Tim Hunt

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

av Dietmar Malli -

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

 

 

Greetings and Thanx Didi

Som svar till Tim Hunt

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

av 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);

Som svar till Alejandro Michavila Pallarés

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

av Dietmar Malli -

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

Som svar till Dietmar Malli

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

av Alejandro Michavila Pallarés -

Try adding this line to your script:

print_object($USER);

And see what happens.

Also increase debug level to developer.

Som svar till Alejandro Michavila Pallarés

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

av 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