Class

eZSession

class eZSession

eZ Publish Session interface class

Session wrapper for session management, with support for handlers. Handler is defined by site.ini[Session]\Handler setting.

The session system has a hook system which allows external code to perform extra tasks before and after a certain action is executed. For instance to cleanup a table when sessions are removed. This can be used by adding a callback with the eZSession::addCallback function, first param is type and second is callback (called with calluserfunc_array)

\code function cleanupStuff( $db, $key, $escKey ) { // Do cleanup here }

eZSession::addCallback( 'destroypre', 'cleanupstuff'); // Or if it was a class function: // eZSession::addCallback( 'destroypre', array('myClass', 'myCleanupStuff') ); \endcode

When a specific session is destroyed in the database it will call the \c destroypre and \c destroypost hooks. The signature of the function is function destroy( $db, $key, $escapedKey )

When a specific session is regenerated (login/logout) and kept it will call \c regeneratepre and \c regeneratepost hooks. The signature of the function is function regenerate( $db, $escapedNewKey, $escapedOldKey, $escUserID )

When multiple sessions are expired (garbage collector) in the database it will call the \c gcpre and \c gcpost hooks. The signature of the function is function gcollect( $db, $expiredTime )

When all sessions are removed from the database it will call the \c cleanuppre and \c cleanuppost hooks. The signature of the function is function cleanup( $db )

\param $db The database object used by the session manager. \param $key The session key which are being worked on, see also \a $escapedKey \param $escapedKey The same key as \a $key but is escaped correctly for the database. Use this to save a call to eZDBInterface::escapeString() \param $expirationTime An integer specifying the timestamp of when the session will expire. \param $expiredTime Similar to \a $expirationTime but is the time used to figure if a session is expired in the database. ie. all sessions with lower expiration times will be removed.

Methods

static mixed|null get(string|null $key = null, null|mixed $defaultValue = null)

Get session value (wrapper)

static bool set($key, $value)

Set session value (wrapper)

static bool|null issetkey(string $key, bool $forceStart = true)

Isset session value (wrapper)

static bool|null unsetkey(string $key, bool $forceStart = true)

unset session value (wrapper)

static bool garbageCollector()

Deletes all expired session data in the database, this function is not supported by session handlers that don't have a session backend on their own.

static bool cleanup()

Truncates all session data in the database, this function is not supported by session handlers that don't have a session backend on their own.

static string countActive()

Counts the number of active session and returns it, this function is not supported by session handlers that don't have a session backend on their own.

static  setCookieLifetime(int $lifetime)

Set the Cookie timeout of session cookie.

static  setCookieParams(int|false $lifetime = false)

Set default cookie parameters based on site.ini settings (fallback to php.ini settings) Used by {@link eZSession::registerFunctions()}.

static bool start(int|false $cookieTimeout = false)

Starts the session and sets the timeout of the session cookie.

static bool|null lazyStart(bool $startIfUserHasCookie = true)

Inits eZSession and starts it if user has cookie and $startIfUserHasCookie is true.

static  init($name, $started, $ns, ezpSessionHandler $handler)

Initializes the legacy session system with injected settings.

static bool stop()

Writes session data and stops the session, if not already stopped.

static bool regenerate(bool $updateBackendData = true)

Will make sure the user gets a new session ID while keepin the session data.

static bool remove()

Removes the current session and resets session variables.

static  setUserID(int $userID)

Sets the current userID used by ezpSessionHandlerDB::write() on shutdown.

static int userID()

Gets the current user id.

static bool|null userHasSessionCookie()

Returns if user had session cookie at start of request or not.

static bool hasStarted()

Return value to indicate if session has started or not

static ezpSessionHandler getHandlerInstance(ezpSessionHandler $handler = null)

Get curren session handler

static  addCallback(string $type, handler $callback)

Adds a callback function, to be triggered by {@link eZSession::triggerCallback()} when a certain session event occurs.

static bool triggerCallback(string $type, array $params)

Triggers callback functions by type, registrated by {@link eZSession::addCallback()} Use: eZSession::triggerCallback('gc_pre', array( $db, $time ) );

Details

at line 141
static public mixed|null get(string|null $key = null, null|mixed $defaultValue = null)

Get session value (wrapper)

Parameters

string|null $key Return the whole session array if null otherwise the value of $key
null|mixed $defaultValue Return this if not null and session has not started

Return Value

mixed|null $defaultValue if key does not exist, otherwise session value depending on $key

at line 165
static public bool set($key, $value)

Set session value (wrapper)

Parameters

$key
$value

Return Value

bool

at line 185
static public bool|null issetkey(string $key, bool $forceStart = true)

Isset session value (wrapper)

Parameters

string $key
bool $forceStart Force session start if true

Return Value

bool|null Null if session has not started and $forceStart is false

at line 206
static public bool|null unsetkey(string $key, bool $forceStart = true)

unset session value (wrapper)

Parameters

string $key
bool $forceStart Force session start if true

Return Value

bool|null True if value was removed, false if it did not exist and null if session is not started and $forceStart is false

at line 230
static public bool garbageCollector()

Deletes all expired session data in the database, this function is not supported by session handlers that don't have a session backend on their own.

Return Value

bool

at line 242
static public bool cleanup()

Truncates all session data in the database, this function is not supported by session handlers that don't have a session backend on their own.

Return Value

bool

at line 254
static public string countActive()

Counts the number of active session and returns it, this function is not supported by session handlers that don't have a session backend on their own.

Return Value

string Number of sessions.

at line 316
static public setCookieLifetime(int $lifetime)

Set the Cookie timeout of session cookie.

Parameters

int $lifetime Cookie timeout of the session cookie

at line 330
static public setCookieParams(int|false $lifetime = false)

Set default cookie parameters based on site.ini settings (fallback to php.ini settings) Used by {@link eZSession::registerFunctions()}.

If you only want to set the cookie lifetime, use {@link eZSession::setCookieLifetime} instead. Note: this will only have affect when session is created / re-created

Parameters

int|false $lifetime Cookie timeout of session cookie, will read from ini if not set

at line 378
static public bool start(int|false $cookieTimeout = false)

Starts the session and sets the timeout of the session cookie.

Multiple calls will be ignored unless you call {@link eZSession::stop()} first.

Parameters

int|false $cookieTimeout Use this to set custom cookie timeout.

Return Value

bool Depending on if session was started.

at line 395
static public bool|null lazyStart(bool $startIfUserHasCookie = true)

Inits eZSession and starts it if user has cookie and $startIfUserHasCookie is true.

Parameters

bool $startIfUserHasCookie

Return Value

bool|null

at line 422
static public init($name, $started, $ns, ezpSessionHandler $handler)

Initializes the legacy session system with injected settings.

This is method is to be used when Symfony2 "drives" the session, it replaces start/lazyStart in such case.

Parameters

$name
$started
$ns
ezpSessionHandler $handler

at line 458
static public bool stop()

Writes session data and stops the session, if not already stopped.

Return Value

bool Depending on if session was stopped.

at line 479
static public bool regenerate(bool $updateBackendData = true)

Will make sure the user gets a new session ID while keepin the session data.

This is useful to call on logins, to avoid sessions theft from users. NOTE: make sure you set new user id first using {@link eZSession::setUserID()}

Parameters

bool $updateBackendData set to false to not update session backend with new session id and user id.

Return Value

bool Depending on if session was regenerated.

at line 506
static public bool remove()

Removes the current session and resets session variables.

Note: implicit stops session as well!

Return Value

bool Depending on if session was removed.

at line 528
static public setUserID(int $userID)

Sets the current userID used by ezpSessionHandlerDB::write() on shutdown.

Parameters

int $userID to use in {@link ezpSessionHandlerDB::write()}

at line 539
static public int userID()

Gets the current user id.

Return Value

int User id stored by {@link eZSession::setUserID()}

at line 550
static public bool|null userHasSessionCookie()

Returns if user had session cookie at start of request or not.

Return Value

bool|null Null if session is not started yet.

at line 561
static public bool hasStarted()

Return value to indicate if session has started or not

Return Value

bool

at line 572
static public ezpSessionHandler getHandlerInstance(ezpSessionHandler $handler = null)

Get curren session handler

Parameters

ezpSessionHandler $handler

Return Value

ezpSessionHandler

at line 614
static public addCallback(string $type, handler $callback)

Adds a callback function, to be triggered by {@link eZSession::triggerCallback()} when a certain session event occurs.

Use: eZSession::addCallback('gc_pre', myCustomGarabageFunction );

Parameters

string $type cleanup, gc, destroy, insert and update, pre and post types.
handler $callback a function to call.

at line 633
static public bool triggerCallback(string $type, array $params)

Triggers callback functions by type, registrated by {@link eZSession::addCallback()} Use: eZSession::triggerCallback('gc_pre', array( $db, $time ) );

Parameters

string $type cleanup, gc, destroy, insert and update, pre and post types.
array $params list of parameters to pass to the callback function.

Return Value

bool