Sessions and moodle

Sessions and moodle

by roy cabaniss -
Number of replies: 8
I've taken the basic moodle user table and modified it (like most folks I suspect.) adding in a student id field and permissions levels amoung other things.

I know I'm generating sessions from moodle because I can get a

print "session id = $PHPSESSID
";

to give me my sessions (which I check via the /tmp directory and evidently are correct.) What I would like is to be able to yank the student id's, usernames and permission levels from the database when they login and store them as session variables for later access and inclusion. (It seems safer to do this than cookies)

Does the moodle login do this and I'm just missing it and if not, suggestions on where to start?
Average of ratings: -
In reply to roy cabaniss

Re: Sessions and moodle

by roy cabaniss -
Ok, taken it a bit further. The base function for the login is in datalib.php under /lib and it's called get_user_info_from_db. The comment before this function goes that suitable for setting as a $user session cookie which is what I am wanting to do. Get some values from the mdl_user table and set them as session variables for later consumption.

Question is, would it be better to do it here or in the setup.php file?

I was thinking just putting a session_register as a part of the get_user_info function. something simple like

session_register("perms");

for the variables I want to carry over. Of course, getting the correct permission levels from the database is the next step.
In reply to roy cabaniss

Re: Sessions and moodle

by John Papaioannou -
You don't need to use session_register at all. Just add the data you wish to $SESSION, e.g.
$SESSION->my_extra_id = $variable;

and it will persist automatically.

In reply to John Papaioannou

Re: Sessions and moodle

by Jamie Pratt -

I think if you want to save things in the session variable you should use :

$SESSION['my_extra_id'] = $variable;

Not the syntax above.

But for what you seem to want to do you should find that setup.php is already reading the extra fields from your modified user table and putting it in $USER. $USER is actually a persistent variable which is stored as a Session variable and $USER contains everything from the user table.

Try print($USER->your_extra_fieldname); or print_r($USER); to see the data from the user table.

Jamie

In reply to Jamie Pratt

Re: Sessions and moodle

by roy cabaniss -
I had thought that would work, in fact that was what I first tried. The suggestion to go with $USER->fieldname

In fact here's what I was using to try to see the stuff (sessions are always initialized in the initial config file first thing.) You can see this at http://cabanisspc.uamont.edu/uam/surveys/
If you want to create an account, tis fine since this is still a work in progress.



Note that this is not within the standard context of moodle but in fact is a try at extending the capabilities. I use a standard moodle install for coursework and am quite happy with that. Still I had thought I could get the data from the table easier than it has come to be.


In reply to roy cabaniss

Re: Sessions and moodle

by Shane Elliott -
Picture of Core developers Picture of Plugin developers
As Jamie pointed out, if you are including the moodle config.php then the $USER variable should be loaded with all the fields from the user table (plus some extra information).

Try the following:
<?php
require('path/to/config/config.php');
print_object($USER);
?>
In reply to Shane Elliott

Re: Sessions and moodle

by roy cabaniss -
I'm beginning to think there is somnething realy whacked in my setup since I agree with all the previous folks. The things folks are telling me should be working. They just aren't. I made a quickie page of the code from shane and called it test. You can see what it gives. I also threw the phpinfo into a page and created a link for folks to take a gander at it (something I almost never do). just to see if someone can spot what I'm missing.

The thing is, it's a vanilla setup from a major distro. Something like this should not be giving us this much problem.
In reply to roy cabaniss

Re: Sessions and moodle

by Jamie Pratt -
I'm not sure exactly what you are trying to do but in order to get access to Moodle session variable you should do something like :


require_once("../../config.php");


This will include setup.php as well. Don't try and start up sessions yourself. Setup.php will do all that for you.
In reply to Jamie Pratt

Bingo !!

by roy cabaniss -
Got it and thanks for all the help. What the problem was (for folks that are trying similar things) is that I was integrating two different sites and was having sessions initialized before the moodle part got it's session start in (since I am using sessions for things like tracking work orders etc) hence moodle's session start was being ignored. I know the solution I will eventually use. Most likely I will use the moodle part for handling the user logins and then define additional session variables as neededfor the work orders, registrations etc.