4 * @file classes/session/SessionDAO.inc.php
6 * Copyright (c) 2000-2009 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
13 * @brief Operations for retrieving and modifying Session objects.
16 // $Id: SessionDAO.inc.php,v 1.6 2009/05/13 16:35:48 asmecher Exp $
19 import('session.Session');
21 class SessionDAO
extends DAO
{
23 * Retrieve a session by ID.
24 * @param $sessionId string
27 function &getSession($sessionId) {
28 $result =& $this->retrieve(
29 'SELECT * FROM sessions WHERE session_id = ?',
34 if ($result->RecordCount() != 0) {
35 $row =& $result->GetRowAssoc(false);
37 $session = new Session();
38 $session->setId($row['session_id']);
39 $session->setUserId($row['user_id']);
40 $session->setIpAddress($row['ip_address']);
41 $session->setUserAgent($row['user_agent']);
42 $session->setSecondsCreated($row['created']);
43 $session->setSecondsLastUsed($row['last_used']);
44 $session->setRemember($row['remember']);
45 $session->setSessionData($row['data']);
56 * Insert a new session.
57 * @param $session Session
59 function insertSession(&$session) {
62 (session_id, ip_address, user_agent, created, last_used, remember, data)
64 (?, ?, ?, ?, ?, ?, ?)',
67 $session->getIpAddress(),
68 $session->getUserAgent(),
69 (int) $session->getSecondsCreated(),
70 (int) $session->getSecondsLastUsed(),
71 $session->getRemember() ?
1 : 0,
72 $session->getSessionData()
78 * Update an existing session.
79 * @param $session Session
81 function updateObject(&$session) {
92 WHERE session_id = ?',
94 $session->getUserId()==''?
null:(int) $session->getUserId(),
95 $session->getIpAddress(),
96 $session->getUserAgent(),
97 (int) $session->getSecondsCreated(),
98 (int) $session->getSecondsLastUsed(),
99 $session->getRemember() ?
1 : 0,
100 $session->getSessionData(),
106 function updateSession(&$session) {
107 trigger_error('Deprecated function.');
108 return $this->updateObject($session);
113 * @param $session Session
115 function deleteObject(&$session) {
116 return $this->deleteSessionById($session->getId());
119 function deleteSession(&$session) {
120 trigger_error('Deprecated function.');
121 return $this->deleteObject($session);
125 * Delete a session by ID.
126 * @param $sessionId string
128 function deleteSessionById($sessionId) {
129 return $this->update(
130 'DELETE FROM sessions WHERE session_id = ?',
136 * Delete sessions by user ID.
137 * @param $userId string
139 function deleteSessionsByUserId($userId) {
140 return $this->update(
141 'DELETE FROM sessions WHERE user_id = ?',
147 * Delete all sessions older than the specified time.
148 * @param $lastUsed int cut-off time in seconds for not-remembered sessions
149 * @param $lastUsedRemember int optional, cut-off time in seconds for remembered sessions
151 function deleteSessionByLastUsed($lastUsed, $lastUsedRemember = 0) {
152 if ($lastUsedRemember == 0) {
153 return $this->update(
154 'DELETE FROM sessions WHERE (last_used < ? AND remember = 0)',
155 array((int) $lastUsed)
158 return $this->update(
159 'DELETE FROM sessions WHERE (last_used < ? AND remember = 0) OR (last_used < ? AND remember = 1)',
160 array((int) $lastUsed, (int) $lastUsedRemember)
166 * Delete all sessions.
168 function deleteAllSessions() {
169 return $this->update('DELETE FROM sessions');
173 * Check if a session exists with the specified ID.
174 * @param $sessionId string
177 function sessionExistsById($sessionId) {
178 $result =& $this->retrieve(
179 'SELECT COUNT(*) FROM sessions WHERE session_id = ?',
182 $returner = isset($result->fields
[0]) && $result->fields
[0] == 1 ?
true : false;