3 namespace Friendica\Core\Session;
5 use Friendica\BaseObject;
6 use Friendica\Core\Session;
7 use Friendica\Database\DBM;
8 use SessionHandlerInterface;
11 require_once 'boot.php';
12 require_once 'include/dba.php';
13 require_once 'include/text.php';
16 * SessionHandler using database
18 * @author Hypolite Petovan <mrpetovan@gmail.com>
20 class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterface
22 public function open($save_path, $session_name)
27 public function read($session_id)
29 if (!x($session_id)) {
33 $session = dba::selectFirst('session', ['data'], ['sid' => $session_id]);
34 if (DBM::is_result($session)) {
35 Session::$exists = true;
36 return $session['data'];
38 logger("no data for session $session_id", LOGGER_TRACE);
44 * @brief Standard PHP session write callback
46 * This callback updates the DB-stored session data and/or the expiration depending
47 * on the case. Uses the Session::expire global for existing session, 5 minutes
48 * for newly created session.
50 * @param string $session_id Session ID with format: [a-z0-9]{26}
51 * @param string $session_data Serialized session data
52 * @return boolean Returns false if parameters are missing, true otherwise
54 public function write($session_id, $session_data)
64 $expire = time() + Session::$expire;
65 $default_expire = time() + 300;
67 if (Session::$exists) {
68 $fields = ['data' => $session_data, 'expire' => $expire];
69 $condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $session_id, $session_data, $expire];
70 dba::update('session', $fields, $condition);
72 $fields = ['sid' => $session_id, 'expire' => $default_expire, 'data' => $session_data];
73 dba::insert('session', $fields);
79 public function close()
84 public function destroy($id)
86 dba::delete('session', ['sid' => $id]);
90 public function gc($maxlifetime)
92 dba::delete('session', ["`expire` < ?", time()]);