return $id;
}
-# track this user with a session cookie (ie a cookie that goes away when the
-# user closes the browser). The timestamp is how long to track the session in
-# the database. Defaults to one day.
+# start a new session.
+# by default it'll expire in 24 hours regardless of activity.
+# pass both args for a session that lasts longer if active.
+# sessions are tracked with a "session cookie" (dies on browser close)
function session_new($idle_timeout = 86400, $max_timeout = 'same_as_idle') {
if ($max_timeout === 'same_as_idle') {
$max_timeout = $idle_timeout;
'idle_timeout' => $idle_timeout,
'expires' => $now + $idle_timeout,
'expires_max' => $now + $max_timeout,
- 'value' => '{}'
+ 'value' => ''
);
db_insert_assoc('wfpl_sessions', $row);
}
}
-# update the idle_timeout
+# this is a helper function. See session_new()
function session_touch() {
if(!session_exists()) {
return;
$GLOBALS['wfpl_session']['expires_max'] = $row['expires_max'];
$GLOBALS['wfpl_session']['key'] = $session_key;
- if ($row['value'] && is_array($parsed = json_decode($row['value'], true))) {
+ if (strlen($row['value']) && is_array($parsed = json_decode($row['value'], true))) {
$GLOBALS['wfpl_session']['value'] = $parsed;
} else {
$GLOBALS['wfpl_session']['value'] = array();
}
- # this session is not idle (extend if it's extendable)
+ # mark session as not idle
session_touch();
return true;
# internal use only (write session cache to db)
function _sync_session() {
- db_update('wfpl_sessions',
- 'value', json_encode($GLOBALS['wfpl_session']['value']),
- 'where id=%i', $GLOBALS['wfpl_session']['id']
- );
+ if (count($GLOBALS['wfpl_session']['value']) > 0) {
+ $value = json_encode($GLOBALS['wfpl_session']['value']);
+ } else {
+ $value = '';
+ }
+ db_update('wfpl_sessions', 'value', $value, 'where id=%i', $GLOBALS['wfpl_session']['id']);
}
# save data into the session