lib/ezsession/classes/ezsession.php
File containing session interface
- Copyright
- Copyright (C) 1999-2011 eZ Systems AS. All rights reserved.
- License
- eZ Business Use License Agreement Version 2.0
- Package
- lib
- Version
- 4.6.0
\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 call_user_func_array)
\code function cleanupStuff( $db, $key, $escKey ) { // Do cleanup here }
eZSession::addCallback( 'destroy_pre', 'cleanupstuff'); // Or if it was a class function: // eZSession::addCallback( 'destroy_pre', array('myClass', 'myCleanupStuff') ); \endcode
When a specific session is destroyed in the database it will call the \c destroy_pre and \c destroy_post 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 regenerate_pre and \c regenerate_post 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 gc_pre and \c gc_post hooks. The signature of the function is function gcollect( $db, $expiredTime )
When all sessions are removed from the database it will call the \c cleanup_pre and \c cleanup_post 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.
Properties


array
$callbackFunctions= 'array()'
List of callback actions, see {@link eZSession::addCallback()}.
array()
Details- Type
- array


\ezpSessionHandler|null
$handlerInstance= 'null'
Current session handler or false, see {@link eZSession::getHandlerInstance()}.
null
Details- Type
- \ezpSessionHandler | null


bool|null
$hasSessionCookie= 'null'
Flag request contains session cookie, set in {@link eZSession::registerFunctions()}.
null
Details- Type
- bool | null


bool
$hasStarted= 'false'
Flag session has started, see {@link eZSession::start()}.
false
Details- Type
- bool
Methods


addCallback(
string $type, \handler $callback
)
:
void
Adds a callback function, to be triggered by {@link eZSession::triggerCallback()} when a certain session event occurs.
Use: eZSession::addCallback('gc_pre', myCustomGarabageFunction );
Name | Type | Description |
---|---|---|
$type | string | cleanup, gc, destroy, insert and update, pre and post types. |
$callback | \handler | a function to call. |
- Deprecated
- since 4.5, use {@link ezpEvent::getInstance()->attach()} with new events
- Since
- 4.1


cleanup(
)
:
bool
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.
Type | Description |
---|---|
bool |
- Since
- 4.1


countActive(
)
:
string
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.
Type | Description |
---|---|
string | Number of sessions. |
- Since
- 4.1


forceStart(
)
:
true
See {@link eZSession::start()}
Type | Description |
---|---|
true |
- Since
- 4.4


garbageCollector(
)
:
bool
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.
Type | Description |
---|---|
bool |
- Since
- 4.1


get(
string | null $key
=
null, null | mixed $defaultValue
=
null
)
:
mixed | null
Get session value (wrapper)
Name | Type | Description |
---|---|---|
$key | string | null | Return the whole session array if null otherwise the value of $key |
$defaultValue | null | mixed | Return this if not null and session has not started |
Type | Description |
---|---|
mixed | null | $defaultValue if key does not exist, otherwise session value depending on $key |
- Since
- 4.4


getHandlerInstance(
)
:
\ezpSessionHandler
Get curren session handler
Type | Description |
---|---|
\ezpSessionHandler |
- Since
- 4.4


getUserSessionHash(
)
:
string
Gets/generates the user hash for use in validating the session based on [Session] SessionValidation* site.ini settings. The default hash is result of md5('empty').
Type | Description |
---|---|
string | MD5 hash based on parts of the user ip and agent string. |
- Deprecated
- as of 4.4, only returns default md5('empty') hash now for BC.
- Since
- 4.1


hasStarted(
)
:
bool
Return value to indicate if session has started or not
Type | Description |
---|---|
bool |
- Since
- 4.4


issetkey(
string $key, bool $forceStart
=
true
)
:
bool | null
Isset session value (wrapper)
Name | Type | Description |
---|---|---|
$key | string | |
$forceStart | bool | Force session start if true |
Type | Description |
---|---|
bool | null | Null if session has not started and $forceStart is false |
- Since
- 4.4


lazyStart(
bool $startIfUserHasCookie
=
true
)
:
bool | null
Inits eZSession and starts it if user has cookie and $startIfUserHasCookie is true.
Name | Type | Description |
---|---|---|
$startIfUserHasCookie | bool |
Type | Description |
---|---|
bool | null |
- Since
- 4.4


regenerate(
bool $updateBackendData
=
true
)
:
bool
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()}
Name | Type | Description |
---|---|---|
$updateBackendData | bool | set to false to not update session backend with new session id and user id. |
Type | Description |
---|---|
bool | Depending on if session was regenerated. |
- Since
- 4.1


registerFunctions(
)
:
bool
Register the needed session functions, this is called automatically by {@link eZSession::start()}, so only call this if you don't start the session.
Type | Description |
---|---|
bool | Depending on if eZSession is registrated as session handler. |
- Since
- 4.1


remove(
)
:
bool
Removes the current session and resets session variables.
Note: implicit stops session as well!
Type | Description |
---|---|
bool | Depending on if session was removed. |
- Since
- 4.1


set(
string $key, $value
)
:
bool
Set session value (wrapper)
Name | Type | Description |
---|---|---|
$key | string | |
$value |
Type | Description |
---|---|
bool |
- Since
- 4.4


setCookieParams(
int | false $lifetime
=
false
)
:
void
Set default cookie parameters based on site.ini settings (fallback to php.ini settings) Used by {@link eZSession::registerFunctions()} Note: this will only have affect when session is created / re-created
Name | Type | Description |
---|---|---|
$lifetime | int | false | Cookie timeout of session cookie, will read from ini if not set |
- Since
- 4.4


setUserID(
int $userID
)
:
void
Sets the current userID used by ezpSessionHandlerDB::write() on shutdown.
Name | Type | Description |
---|---|---|
$userID | int | to use in {@link ezpSessionHandlerDB::write()} |
- Since
- 4.1


start(
int | false $cookieTimeout
=
false
)
:
bool
Starts the session and sets the timeout of the session cookie.
Multiple calls will be ignored unless you call {@link eZSession::stop()} first.
Name | Type | Description |
---|---|---|
$cookieTimeout | int | false | Use this to set custom cookie timeout. |
Type | Description |
---|---|
bool | Depending on if session was started. |
- Since
- 4.1


stop(
)
:
bool
Writes session data and stops the session, if not already stopped.
Type | Description |
---|---|
bool | Depending on if session was stopped. |
- Since
- 4.1


triggerCallback(
string $type, array $params
)
:
bool
Triggers callback functions by type, registrated by {@link eZSession::addCallback()} Use: eZSession::triggerCallback('gc_pre', array( $db, $time ) );
Name | Type | Description |
---|---|---|
$type | string | cleanup, gc, destroy, insert and update, pre and post types. |
$params | array | list of parameters to pass to the callback function. |
Type | Description |
---|---|
bool |
- Deprecated
- since 4.5, use {@link ezpEvent::getInstance()->notify()} with new events
- Since
- 4.1


unsetkey(
string $key, bool $forceStart
=
true
)
:
bool | null
unset session value (wrapper)
Name | Type | Description |
---|---|---|
$key | string | |
$forceStart | bool | Force session start if true |
Type | Description |
---|---|
bool | null | True if value was removed, false if it did not exist and null if session is not started and $forceStart is false |
- Since
- 4.4


userHasSessionCookie(
)
:
bool | null
Returns if user had session cookie at start of request or not.
Type | Description |
---|---|
bool | null | Null if session is not started yet. |
- Since
- 4.1


userID(
)
:
int
Gets the current user id.
Type | Description |
---|---|
int | User id stored by {@link eZSession::setUserID()} |
- Since
- 4.1


userSessionIsValid(
)
:
bool | null
Returns if user session validated against stored data in db or if it was invalidated during the current request.
Type | Description |
---|---|
bool | null | Null if user is not validated yet (for instance a new session). |
- Deprecated
- as of 4.4, only returns true for bc
- Since
- 4.1