From d0f6c42b62f655b936e700c849168512e39af8f6 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Tue, 31 Mar 2015 10:28:27 -0400 Subject: [PATCH] update db encoding of empty session, docs --- session.php | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/session.php b/session.php index 87cbfd5..3b96e84 100644 --- a/session.php +++ b/session.php @@ -57,9 +57,10 @@ function session_generate_key() { 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; @@ -74,7 +75,7 @@ function session_new($idle_timeout = 86400, $max_timeout = 'same_as_idle') { 'idle_timeout' => $idle_timeout, 'expires' => $now + $idle_timeout, 'expires_max' => $now + $max_timeout, - 'value' => '{}' + 'value' => '' ); db_insert_assoc('wfpl_sessions', $row); @@ -101,7 +102,7 @@ function session_set_cookie() { } } -# update the idle_timeout +# this is a helper function. See session_new() function session_touch() { if(!session_exists()) { return; @@ -166,13 +167,13 @@ function session_exists() { $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; @@ -234,10 +235,12 @@ function init_session() { # 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 -- 1.7.10.4