Session handling: Difference between revisions
Patryk Szuta (talk | contribs) m (Moodle 2.7 and 2.8 removed the ini_set option for $CFG->session_memcached_number_of_replicas in lib/classes/session/memcached.php.) |
|||
(17 intermediate revisions by 8 users not shown) | |||
Line 1: | Line 1: | ||
{{Server settings}} | {{Server settings}} | ||
An administrator can change the following settings in '' | An administrator can change the following settings in ''Administration > Site administration > Server > Session Handling''. | ||
Once someone logs in to your Moodle server, the server starts a session. The session data allows the server to track users as they access different pages. | |||
==Use database for session information== | |||
Moodle needs to store the session data in some storage. By default either file or database session storage is selected, this option allows admin to change it. Large installation should use memcached driver described below. | |||
Note that this option disappears after setting the $CFG->session_handler_class in config.php file. | |||
==Timeout== | ==Timeout== | ||
If users don't load a new page during the amount of time set here, Moodle will end their session and log them out. | |||
Be sure this time frame is long enough to cover the longest test your teachers may offer. If a student is logged out while they are taking a test, their responses to the test questions may be lost. | Be sure this time frame is long enough to cover the longest test your teachers may offer. If a student is logged out while they are taking a test, their responses to the test questions may be lost. | ||
Line 13: | Line 21: | ||
Note: If you change "Cookie prefix" or "Cookie path" you will need to login again as the changes take effect immediately. | Note: If you change "Cookie prefix" or "Cookie path" you will need to login again as the changes take effect immediately. | ||
==Cookie path== | |||
The relative path to this Moodle installation, this may be used to force sending of Moodle session cookie to parent directories. Invalid values are ignored automatically. | |||
==Cookie domain== | |||
This can be used to send session cookies to higher domains instead of just the server domain. This may be useful for some SSO solutions. Invalid values are ignored automatically. | |||
==Session drivers== | ==Session drivers== | ||
User sessions may be stored in different backends. Session drivers can be configured only in config.php file - see examples in config-dist.php file. | User sessions may be stored in different backends. Session drivers can be configured only in config.php file - see examples in config-dist.php file. | ||
=== | ===Memcached=== | ||
Memcached session driver is the fastest driver, it requires external | Memcached session driver is the fastest driver, it requires external memcached server and memcached PHP extension. Server cluster nodes must use shared session storage. | ||
Configuration options in config.php: | Configuration options in config.php: | ||
Line 26: | Line 42: | ||
$CFG->session_memcached_prefix = 'memc.sess.key.'; | $CFG->session_memcached_prefix = 'memc.sess.key.'; | ||
$CFG->session_memcached_acquire_lock_timeout = 120; | $CFG->session_memcached_acquire_lock_timeout = 120; | ||
$CFG->session_memcached_lock_expire = 7200; // Ignored if memcached extension <= 2.1.0 | |||
</code> | </code> | ||
Notes: | Notes: | ||
* Make sure the memcached server has enough memory. | * Make sure the memcached server has enough memory. | ||
* Use different prefix when storing sessions from multiple | * Use different prefix when storing sessions from multiple Moodles in one server. | ||
* | * If PECL memcached extension version installed is less that 2.2.0, the locking works differently from other drivers - the lock is expired/released at the end of timeout - see MDL-42485. | ||
* '''Don't use the same memcached server for both sessions and MUC. Events triggering MUC caches to be purged leads to MUC purging the memcached server - thus terminating ALL sessions.''' | |||
* The <code php>$CFG->session_memcached_number_of_replicas</code> option is no longer supported. | |||
For windows users, PHP.net only supplies binaries for memcache, (and not memcached). (http://windows.php.net/downloads/pecl/releases/) | |||
(As of 2.7, two different contribs exist for memcache session handling - see MDL-42011 - it seems the OU one doesn't use prefix/lock_expire for some reason... possibly better to use the catalyst patch, where the only difference to the above config.php is the spelling of memcache(d)) | |||
===Files=== | ===Files=== | ||
Line 43: | Line 67: | ||
Notes: | Notes: | ||
* File based | * File based sessions require file system that supports file locking. | ||
===Database=== | ===Database=== | ||
This driver was used by default in Moodle 2.0-2.5 | This type of driver was used by default in Moodle 2.0-2.5 | ||
<code php> | <code php> | ||
Line 56: | Line 79: | ||
Notes: | Notes: | ||
* DB sessions are not compatible with MyISAM database engine. | * DB sessions are not compatible with MyISAM database engine. | ||
* If you are using MySQL/MariaDB make sure that \'max_allowed_packet\' in my.cnf (or my.ini) is at least 4M. | |||
* The performance is relatively low, it is not recommended for large sites. | |||
==See also== | ==See also== |
Latest revision as of 18:23, 5 December 2014
An administrator can change the following settings in Administration > Site administration > Server > Session Handling.
Once someone logs in to your Moodle server, the server starts a session. The session data allows the server to track users as they access different pages.
Use database for session information
Moodle needs to store the session data in some storage. By default either file or database session storage is selected, this option allows admin to change it. Large installation should use memcached driver described below.
Note that this option disappears after setting the $CFG->session_handler_class in config.php file.
Timeout
If users don't load a new page during the amount of time set here, Moodle will end their session and log them out.
Be sure this time frame is long enough to cover the longest test your teachers may offer. If a student is logged out while they are taking a test, their responses to the test questions may be lost.
Cookie prefix
Most of the time, you can leave this blank, unless you are running more than one Moodle site on the same server. In this case, you will want to customize the name of the cookie each Moodle site uses to track the session. This enables you to be logged into more than one Moodle site at the same time.
Note: If you change "Cookie prefix" or "Cookie path" you will need to login again as the changes take effect immediately.
Cookie path
The relative path to this Moodle installation, this may be used to force sending of Moodle session cookie to parent directories. Invalid values are ignored automatically.
Cookie domain
This can be used to send session cookies to higher domains instead of just the server domain. This may be useful for some SSO solutions. Invalid values are ignored automatically.
Session drivers
User sessions may be stored in different backends. Session drivers can be configured only in config.php file - see examples in config-dist.php file.
Memcached
Memcached session driver is the fastest driver, it requires external memcached server and memcached PHP extension. Server cluster nodes must use shared session storage.
Configuration options in config.php:
$CFG->session_handler_class = '\core\session\memcached';
$CFG->session_memcached_save_path = '127.0.0.1:11211';
$CFG->session_memcached_prefix = 'memc.sess.key.';
$CFG->session_memcached_acquire_lock_timeout = 120;
$CFG->session_memcached_lock_expire = 7200; // Ignored if memcached extension <= 2.1.0
Notes:
- Make sure the memcached server has enough memory.
- Use different prefix when storing sessions from multiple Moodles in one server.
- If PECL memcached extension version installed is less that 2.2.0, the locking works differently from other drivers - the lock is expired/released at the end of timeout - see MDL-42485.
- Don't use the same memcached server for both sessions and MUC. Events triggering MUC caches to be purged leads to MUC purging the memcached server - thus terminating ALL sessions.
- The
$CFG->session_memcached_number_of_replicas
option is no longer supported.
For windows users, PHP.net only supplies binaries for memcache, (and not memcached). (http://windows.php.net/downloads/pecl/releases/)
(As of 2.7, two different contribs exist for memcache session handling - see MDL-42011 - it seems the OU one doesn't use prefix/lock_expire for some reason... possibly better to use the catalyst patch, where the only difference to the above config.php is the spelling of memcache(d))
Files
This driver is used by default in new installation.
Configuration options in config.php:
$CFG->session_handler_class = '\core\session\file';
$CFG->session_file_save_path = $CFG->dataroot.'/sessions';
Notes:
- File based sessions require file system that supports file locking.
Database
This type of driver was used by default in Moodle 2.0-2.5
$CFG->session_handler_class = '\core\session\database';
$CFG->session_database_acquire_lock_timeout = 120;
Notes:
- DB sessions are not compatible with MyISAM database engine.
- If you are using MySQL/MariaDB make sure that \'max_allowed_packet\' in my.cnf (or my.ini) is at least 4M.
- The performance is relatively low, it is not recommended for large sites.