General developer forum

Moodle Session Data

Picture of Matt McCarty
Moodle Session Data

I have seen this question posted before, but I can't seem to find a good solution for my particular problem.

I am using Moodle 2.0. Moodle is installed in the root /public_html/ directory. In root install directory, I have a custom directory called "mysystem". The "mysystem" directory holds a custom system that is used for online training.

Everything works, meaning I can access everything in "mysystem" and it displays correctly. Though, what I need to do is edit the custom system (in the "mysystem" directory) to check if the user is logged into moodle and if not, redirect the user to the moodle login page.

This process is easy enough, but I cannot access anything contained in the moodle $_SESSION variable. I have called session_start() at the top of my script, but the $_SESSION variable is still empty even when a moodle user is logged in.

In several forum posts, I have read that "when trying to access moodle session data from outside of moodle, the easiest thing to do is include the moodle config.php into your script", yet this solution is not working. when including the moodle config I get a blank screen (some un-recognized error). Another solution I came across was to "convert" my custom system into a moodle plugin, but my custom system will have nothing to do with just needs to check if a user is logged into moodle.

I hope this makes sense. Does anybody have any suggestions?


Thanks for the help!

Average of ratings: -
Anne Krijger
Re: Moodle Session Data

Hi Matt,

Assuming your 'mysystem' uses the same session store as Moodle, you should be able to use the session by passing the relative session id to your system using the url.


Average of ratings: -
Picture of The Tinman
Re: Moodle Session Data


Could you give me an idea of how exactly I'd do this?

In the past I've used sesions along the lines of $name = $_Session['name']; but I'm not sure how to pull the session data from Moodle's session.  I feel like I'm doing the same thing the original poster is, I have a folder within my moodle structure called intranet, and when a user clicks a link from within moodle, it will pull up http://moodlesite.local/intranet/form.php.  On the form page, I just want it to show the same information I can see while in Moodle, "You are logged in as <username>".  I've been sitting here playing around with $_Session, but I feel like I'm missing something crucial.

Average of ratings: -
Picture of The Tinman
Re: Moodle Session Data

I think I answered my own question, but it's left me more confused.

I created a page accessible after I log into moodle called session_inspect.php.  On that page I wrote...





but i just get an empty array, which makes feel like my login information isn't even a session cookie?!   Can someone push me in the right direction here?





added require_once('./config.php'); right after <?php, and all is wonderful now!

Average of ratings: -