Session variable is unset in moodle pages

Session variable is unset in moodle pages

by Raphael Goldman -
Number of replies: 6

Hi,

I have a value in a session variable,

if I try to trying to print the content of the variable in a test file I have a value in the variable.

If I try printing the variable from a page of moodle the variable is unset, is there any way moodle is killing all session variables that he doesn't need?

This is the process I am trying to do,

I have a table of all the users in the course the teacher click on one of them and then the id of the student is sent to a php file and start a session variable with the userid, after I click on userid 5 for example I can see this value only in new pages that I create but not in moodle pages, any idea how I can solve the problem?

 

If I put the code in the head of a moodle page then I can see the value only if I kill the page after I print the value,

otherwise I get an error:

Debug info: 
Error code: dbsessionhandlerproblem
Stack trace:
  • line 476 of /lib/setuplib.php: moodle_exception thrown
  • line 530 of /lib/sessionlib.php: call to print_error()
  • line 182 of /lib/sessionlib.php: call to database_session->init_session_storage()
  • line 482 of /lib/sessionlib.php: call to session_stub->__construct()
  • line 66 of /lib/sessionlib.php: call to database_session->__construct()
  • line 755 of /lib/setup.php: call to session_get_instance()
  • line 25 of /config.php: call to require_once()
  • line 24 of /mod/scorm/view.php: call to require_once()

 

Average of ratings: -
In reply to Raphael Goldman

תשובה ל: Session variable is unset in moodle pages

by Raphael Goldman -

I solved the problem,

I used :

session_name('MoodleSession');

before using 

session_start()

and now it is working

In reply to Raphael Goldman

Re: תשובה ל: Session variable is unset in moodle pages

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

You should not be calling session_name or session_start yourself. Moodle takes care of that. Just do require_once config.php and require_login to get $SESSION set up.

In reply to Tim Hunt

Re: תשובה ל: Session variable is unset in moodle pages

by Nathan Mcilree -

Hi Tim, I would like to override some of the MOODLE $CFG-> variables set in config.php based on a $_GET variable sent to MOODLE as part of a user redirect from another site. However, any session variables set in config.php are not being passed to subsequent pages.  Is it possible to persist a session in config.php?

In reply to Tim Hunt

Re: תשובה ל: Session variable is unset in moodle pages

by Sen G -

We are exposing moodle data via some custom APIs. I included config.php and sessionlib.php and tried $SESSION to add some additional data, it works only when used outside of my custom class. When used within a class, it doesn't set. I'm not sure Moodle resets $SESSION in the next request.  I tried several ways, but I couldn't set session.

In reply to Sen G

Re: תשובה ל: Session variable is unset in moodle pages

by Darko Miletić -

If this is the code called from outside of Moodle than session would not work since session implies loggedin user. If you need to preserve some value across different sessions use custom configuration setting. See set_config api.