Properties

$logger

$logger : \PSR\Log\LoggerInterface

Logger instance to record error messages and warnings.

Type

\PSR\Log\LoggerInterface

$sessionDriverName

$sessionDriverName : string

The storage driver to use: files, database, redis, memcached

Type

string

$sessionCookieName

$sessionCookieName : string

The session cookie name, must contain only [0-9a-z_-] characters.

Type

string

$sessionExpiration

$sessionExpiration : integer

The number of SECONDS you want the session to last.

Setting it to 0 (zero) means expire when the browser is closed.

Type

integer

$sessionSavePath

$sessionSavePath : string

The location to save sessions to, driver dependent.

.

For the 'files' driver, it's a path to a writable directory. WARNING: Only absolute paths are supported!

For the 'database' driver, it's a table name.

TODO: address memcache & redis needs

IMPORTANT: You are REQUIRED to set a valid save path!

Type

string

$sessionMatchIP

$sessionMatchIP : boolean

Whether to match the user's IP address when reading the session data.

WARNING: If you're using the database driver, don't forget to update your session table's PRIMARY KEY when changing this setting.

Type

boolean

$sessionTimeToUpdate

$sessionTimeToUpdate : integer

How many seconds between CI regenerating the session ID.

Type

integer

$sessionRegenerateDestroy

$sessionRegenerateDestroy : boolean

Whether to destroy session data associated with the old session ID when auto-regenerating the session ID. When set to FALSE, the data will be later deleted by the garbage collector.

Type

boolean

$cookieDomain

$cookieDomain : string

The domain name to use for cookies.

Set to .your-domain.com for site-wide cookies.

Type

string

$cookiePath

$cookiePath : string

Path used for storing cookies.

Typically will be a forward slash.

Type

string

$cookieSecure

$cookieSecure : boolean

Cookie will only be set if a secure HTTPS connection exists.

Type

boolean

$sidRegexp

$sidRegexp : string

sid regex expression

Type

string

Methods

__construct()

__construct(\SessionHandlerInterface  $driver, \Config\App  $config) 

Constructor.

Extract configuration settings and save them here.

Parameters

\SessionHandlerInterface $driver
\Config\App $config

start()

start() : mixed

Initialize the session container and starts up the session.

Returns

mixed

stop()

stop() 

Does a full stop of the session:

  • destroys the session
  • unsets the session id
  • destroys the session cookie

regenerate()

regenerate(boolean  $destroy = false) 

Regenerates the session ID.

Parameters

boolean $destroy

Should old session data be destroyed?

destroy()

destroy() 

Destroys the current session.

set()

set(string|array  $data, string|array  $value = null) 

Sets user data into the session.

If $data is a string, then it is interpreted as a session property key, and $value is expected to be non-null.

If $data is an array, it is expected to be an array of key/value pairs to be set as session properties.

Parameters

string|array $data

Property name or associative array of properties

string|array $value

Property value if single key provided

get()

get(string  $key = null) : array|null

Get user data that has been set in the session.

If the property exists as "normal", returns it. Otherwise, returns an array of any temp or flash data values with the property key.

Replaces the legacy method $session->userdata();

Parameters

string $key

Identifier of the session property to retrieve

Returns

array|null —

The property value(s)

has()

has(string  $key) : boolean

Returns whether an index exists in the session array.

Parameters

string $key

Identifier of the session property we are interested in.

Returns

boolean

push()

push(string  $key, array  $data) : void

Push new value onto session value that is array.

Parameters

string $key

Identifier of the session property we are interested in.

array $data

value to be pushed to existing session key.

remove()

remove(string|array  $key) 

Remove one or more session properties.

If $key is an array, it is interpreted as an array of string property identifiers to remove. Otherwise, it is interpreted as the identifier of a specific session property to remove.

Parameters

string|array $key

Identifier of the session property or properties to remove.

__set()

__set(string  $key, string|array  $value) 

Magic method to set variables in the session by simply calling $session->foo = bar;

Parameters

string $key

Identifier of the session property to set.

string|array $value

__get()

__get(string  $key) : null|string

Magic method to get session variables by simply calling $foo = $session->foo;

Parameters

string $key

Identifier of the session property to remove.

Returns

null|string

__isset()

__isset(string  $key) : boolean

Magic method to check for session variables.

Different from has() in that it will validate 'session_id' as well. Mostly used by internal PHP functions, users should stick to has()

Parameters

string $key

Identifier of the session property to remove.

Returns

boolean

setFlashdata()

setFlashdata(array|string  $data, string|array  $value = null) 

Sets data into the session that will only last for a single request.

Perfect for use with single-use status update messages.

If $data is an array, it is interpreted as an associative array of key/value pairs for flashdata properties. Otherwise, it is interpreted as the identifier of a specific flashdata property, with $value containing the property value.

Parameters

array|string $data

Property identifier or associative array of properties

string|array $value

Property value if $data is a scalar

getFlashdata()

getFlashdata(string  $key = null) : array|null

Retrieve one or more items of flash data from the session.

If the item key is null, return all flashdata.

Parameters

string $key

Property identifier

Returns

array|null —

The requested property value, or an associative array of them

keepFlashdata()

keepFlashdata(array|string  $key) 

Keeps a single piece of flash data alive for one more request.

Parameters

array|string $key

Property identifier or array of them

markAsFlashdata()

markAsFlashdata(array|string  $key) : boolean

Mark a session property or properties as flashdata.

Parameters

array|string $key

Property identifier or array of them

Returns

boolean —

False if any of the properties are not already set

unmarkFlashdata()

unmarkFlashdata(mixed  $key) 

Unmark data in the session as flashdata.

Parameters

mixed $key

Property identifier or array of them

getFlashKeys()

getFlashKeys() : array

Retrieve all of the keys for session data marked as flashdata.

Returns

array —

The property names of all flashdata

setTempdata()

setTempdata(string|array  $data, null  $value = null, integer  $ttl = 300) 

Sets new data into the session, and marks it as temporary data with a set lifespan.

Parameters

string|array $data

Session data key or associative array of items

null $value

Value to store

integer $ttl

Time-to-live in seconds

getTempdata()

getTempdata(string  $key = null) : mixed

Returns either a single piece of tempdata, or all temp data currently in the session.

Parameters

string $key

Session data key

Returns

mixed —

Session data value or null if not found.

removeTempdata()

removeTempdata(string  $key) 

Removes a single piece of temporary data from the session.

Parameters

string $key

Session data key

markAsTempdata()

markAsTempdata(string|array  $key, integer  $ttl = 300) : boolean

Mark one of more pieces of data as being temporary, meaning that it has a set lifespan within the session.

Parameters

string|array $key

Property identifier or array of them

integer $ttl

Time to live, in seconds

Returns

boolean —

False if any of the properties were not set

unmarkTempdata()

unmarkTempdata(string|array  $key) 

Unmarks temporary data in the session, effectively removing its lifespan and allowing it to live as long as the session does.

Parameters

string|array $key

Property identifier or array of them

getTempKeys()

getTempKeys() : array

Retrieve the keys of all session data that have been marked as temporary data.

Returns

array

configure()

configure() 

Configuration.

Handle input binds and configuration defaults.

configureSidLength()

configureSidLength() : void

Configure session ID length

To make life easier, we used to force SHA-1 and 4 bits per character on everyone. And of course, someone was unhappy.

Then PHP 7.1 broke backwards-compatibility because ext/session is such a mess that nobody wants to touch it with a pole stick, and the one guy who does, nobody has the energy to argue with.

So we were forced to make changes, and OF COURSE something was going to break and now we have this pile of shit. -- Narf

initVars()

initVars() 

Handle temporary variables

Clears old "flash" data, marks the new one for deletion and handles "temp" data deletion.

setSaveHandler()

setSaveHandler() 

Sets the driver as the session handler in PHP.

Extracted for easier testing.

startSession()

startSession() 

Starts the session.

Extracted for testing reasons.

setCookie()

setCookie() 

Takes care of setting the cookie on the client side.

Extracted for testing reasons.