3 namespace Friendica\Core\Session;
5 use Friendica\BaseObject;
6 use Friendica\Core\Session;
7 use Friendica\Database\DBA;
8 use SessionHandlerInterface;
10 require_once 'boot.php';
11 require_once 'include/dba.php';
12 require_once 'include/text.php';
15 * SessionHandler using database
17 * @author Hypolite Petovan <hypolite@mrpetovan.com>
19 class DatabaseSessionHandler extends BaseObject implements SessionHandlerInterface
21 public function open($save_path, $session_name)
26 public function read($session_id)
28 if (!x($session_id)) {
32 $session = DBA::selectFirst('session', ['data'], ['sid' => $session_id]);
33 if (DBA::isResult($session)) {
34 Session::$exists = true;
35 return $session['data'];
37 logger("no data for session $session_id", LOGGER_TRACE);
43 * @brief Standard PHP session write callback
45 * This callback updates the DB-stored session data and/or the expiration depending
46 * on the case. Uses the Session::expire global for existing session, 5 minutes
47 * for newly created session.
49 * @param string $session_id Session ID with format: [a-z0-9]{26}
50 * @param string $session_data Serialized session data
51 * @return boolean Returns false if parameters are missing, true otherwise
53 public function write($session_id, $session_data)
63 $expire = time() + Session::$expire;
64 $default_expire = time() + 300;
66 if (Session::$exists) {
67 $fields = ['data' => $session_data, 'expire' => $expire];
68 $condition = ["`sid` = ? AND (`data` != ? OR `expire` != ?)", $session_id, $session_data, $expire];
69 DBA::update('session', $fields, $condition);
71 $fields = ['sid' => $session_id, 'expire' => $default_expire, 'data' => $session_data];
72 DBA::insert('session', $fields);
78 public function close()
83 public function destroy($id)
85 DBA::delete('session', ['sid' => $id]);
89 public function gc($maxlifetime)
91 DBA::delete('session', ["`expire` < ?", time()]);