3 // Session management functions. These provide database storage of PHP
7 $session_expire = 180000;
9 if(! function_exists('ref_session_open')) {
10 function ref_session_open ($s,$n) {
14 if(! function_exists('ref_session_read')) {
15 function ref_session_read ($id) {
16 global $session_exists;
18 $r = q("SELECT `data` FROM `session` WHERE `sid`= '%s'", dbesc($id));
21 $session_exists = true;
24 logger("no data for session $id", LOGGER_TRACE);
29 if(! function_exists('ref_session_write')) {
30 function ref_session_write ($id,$data) {
31 global $session_exists, $session_expire;
33 if(! $id || ! $data) {
37 $expire = time() + $session_expire;
38 $default_expire = time() + 300;
41 $r = q("UPDATE `session`
43 WHERE `data` != '%s' AND `sid` = '%s'",
44 dbesc($data), dbesc($data), dbesc($id));
46 $r = q("UPDATE `session`
48 WHERE `expire` != '%s' AND `sid` = '%s'",
49 dbesc($expire), dbesc($expire), dbesc($id));
51 $r = q("INSERT INTO `session`
52 SET `sid` = '%s', `expire` = '%s', `data` = '%s'",
53 dbesc($id), dbesc($default_expire), dbesc($data));
58 if(! function_exists('ref_session_close')) {
59 function ref_session_close() {
63 if(! function_exists('ref_session_destroy')) {
64 function ref_session_destroy ($id) {
65 q("DELETE FROM `session` WHERE `sid` = '%s'", dbesc($id));
69 if(! function_exists('ref_session_gc')) {
70 function ref_session_gc($expire) {
71 q("DELETE FROM `session` WHERE `expire` < %d", dbesc(time()));
77 ini_set('session.gc_probability', $gc_probability);
78 ini_set('session.use_only_cookies', 1);
79 ini_set('session.cookie_httponly', 1);
81 if (!get_config('system', 'disable_database_session'))
82 session_set_save_handler('ref_session_open', 'ref_session_close',
83 'ref_session_read', 'ref_session_write',
84 'ref_session_destroy', 'ref_session_gc');