Session
in package
implements
SessionInterface
Uses
LoggerAwareTrait
Implementation of CodeIgniter session container.
Session configuration is done through session variables and cookie related variables in app/config/App.php
Interfaces, Classes and Traits
- SessionInterface
- Expected behavior of a session container used with CodeIgniter.
Table of Contents
- $cookie : Cookie
- The session cookie instance.
- $cookieDomain : string
- The domain name to use for cookies.
- $cookiePath : string
- Path used for storing cookies.
- $cookieSameSite : string
- Cookie SameSite setting as described in RFC6265 Must be 'None', 'Lax' or 'Strict'.
- $cookieSecure : bool
- Cookie will only be set if a secure HTTPS connection exists.
- $driver : SessionHandlerInterface
- Instance of the driver to use.
- $logger : LoggerInterface
- Logger instance to record error messages and warnings.
- $sessionCookieName : string
- The session cookie name, must contain only [0-9a-z_-] characters.
- $sessionDriverName : string
- The storage driver to use: files, database, redis, memcached
- $sessionExpiration : int
- The number of SECONDS you want the session to last.
- $sessionMatchIP : bool
- Whether to match the user's IP address when reading the session data.
- $sessionRegenerateDestroy : bool
- 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.
- $sessionSavePath : string
- The location to save sessions to, driver dependent.. For the 'files' driver, it's a path to a writable directory.
- $sessionTimeToUpdate : int
- How many seconds between CI regenerating the session ID.
- $sidRegexp : string
- sid regex expression
- __construct() : mixed
- Constructor.
- __get() : null|string
- Magic method to get session variables by simply calling $foo = $session->foo;
- __isset() : bool
- Magic method to check for session variables.
- __set() : mixed
- Magic method to set variables in the session by simply calling $session->foo = bar;
- destroy() : mixed
- Destroys the current session.
- get() : mixed
- Get user data that has been set in the session.
- getFlashdata() : array<string|int, mixed>|null
- Retrieve one or more items of flash data from the session.
- getFlashKeys() : array<string|int, mixed>
- Retrieve all of the keys for session data marked as flashdata.
- getTempdata() : mixed
- Returns either a single piece of tempdata, or all temp data currently in the session.
- getTempKeys() : array<string|int, mixed>
- Retrieve the keys of all session data that have been marked as temporary data.
- has() : bool
- Returns whether an index exists in the session array.
- keepFlashdata() : mixed
- Keeps a single piece of flash data alive for one more request.
- markAsFlashdata() : bool
- Mark a session property or properties as flashdata.
- markAsTempdata() : bool
- Mark one of more pieces of data as being temporary, meaning that it has a set lifespan within the session.
- push() : void
- Push new value onto session value that is array.
- regenerate() : mixed
- Regenerates the session ID.
- remove() : mixed
- Remove one or more session properties.
- removeTempdata() : mixed
- Removes a single piece of temporary data from the session.
- set() : mixed
- Sets user data into the session.
- setFlashdata() : mixed
- Sets data into the session that will only last for a single request.
- setLogger() : mixed
- Sets a logger.
- setTempdata() : mixed
- Sets new data into the session, and marks it as temporary data with a set lifespan.
- start() : mixed
- Initialize the session container and starts up the session.
- stop() : mixed
- Does a full stop of the session:
- unmarkFlashdata() : mixed
- Unmark data in the session as flashdata.
- unmarkTempdata() : mixed
- Unmarks temporary data in the session, effectively removing its lifespan and allowing it to live as long as the session does.
- configure() : mixed
- Configuration.
- configureSidLength() : void
- Configure session ID length
- initVars() : mixed
- Handle temporary variables
- setCookie() : mixed
- Takes care of setting the cookie on the client side.
- setSaveHandler() : mixed
- Sets the driver as the session handler in PHP.
- startSession() : mixed
- Starts the session.
Properties
$cookie
The session cookie instance.
protected
Cookie
$cookie
$cookieDomain
The domain name to use for cookies.
protected
string
$cookieDomain
= ''
Set to .your-domain.com for site-wide cookies.
Tags
$cookiePath
Path used for storing cookies.
protected
string
$cookiePath
= '/'
Typically will be a forward slash.
Tags
$cookieSameSite
Cookie SameSite setting as described in RFC6265 Must be 'None', 'Lax' or 'Strict'.
protected
string
$cookieSameSite
= CodeIgniterCookieCookie::SAMESITE_LAX
Tags
$cookieSecure
Cookie will only be set if a secure HTTPS connection exists.
protected
bool
$cookieSecure
= false
Tags
$driver
Instance of the driver to use.
protected
SessionHandlerInterface
$driver
$logger
Logger instance to record error messages and warnings.
protected
LoggerInterface
$logger
$sessionCookieName
The session cookie name, must contain only [0-9a-z_-] characters.
protected
string
$sessionCookieName
= 'ci_session'
$sessionDriverName
The storage driver to use: files, database, redis, memcached
protected
string
$sessionDriverName
$sessionExpiration
The number of SECONDS you want the session to last.
protected
int
$sessionExpiration
= 7200
Setting it to 0 (zero) means expire when the browser is closed.
$sessionMatchIP
Whether to match the user's IP address when reading the session data.
protected
bool
$sessionMatchIP
= false
WARNING: If you're using the database driver, don't forget to update your session table's PRIMARY KEY when changing this setting.
$sessionRegenerateDestroy
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.
protected
bool
$sessionRegenerateDestroy
= false
$sessionSavePath
The location to save sessions to, driver dependent.. For the 'files' driver, it's a path to a writable directory.
protected
string
$sessionSavePath
WARNING: Only absolute paths are supported!
For the 'database' driver, it's a table name.
Tags
$sessionTimeToUpdate
How many seconds between CI regenerating the session ID.
protected
int
$sessionTimeToUpdate
= 300
$sidRegexp
sid regex expression
protected
string
$sidRegexp
Methods
__construct()
Constructor.
public
__construct(SessionHandlerInterface $driver, App $config) : mixed
Extract configuration settings and save them here.
Parameters
- $driver : SessionHandlerInterface
- $config : App
Return values
mixed —__get()
Magic method to get session variables by simply calling $foo = $session->foo;
public
__get(string $key) : null|string
Parameters
- $key : string
-
Identifier of the session property to remove.
Return values
null|string —__isset()
Magic method to check for session variables.
public
__isset(string $key) : bool
Different from has() in that it will validate 'session_id' as well. Mostly used by internal PHP functions, users should stick to has()
Parameters
- $key : string
-
Identifier of the session property to remove.
Return values
bool —__set()
Magic method to set variables in the session by simply calling $session->foo = bar;
public
__set(string $key, string|array<string|int, mixed> $value) : mixed
Parameters
- $key : string
-
Identifier of the session property to set.
- $value : string|array<string|int, mixed>
Return values
mixed —destroy()
Destroys the current session.
public
destroy() : mixed
Return values
mixed —get()
Get user data that has been set in the session.
public
get([string|null $key = null ]) : mixed
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
- $key : string|null = null
-
Identifier of the session property to retrieve
Return values
mixed —The property value(s)
getFlashdata()
Retrieve one or more items of flash data from the session.
public
getFlashdata([string $key = null ]) : array<string|int, mixed>|null
If the item key is null, return all flashdata.
Parameters
- $key : string = null
-
Property identifier
Return values
array<string|int, mixed>|null —The requested property value, or an associative array of them
getFlashKeys()
Retrieve all of the keys for session data marked as flashdata.
public
getFlashKeys() : array<string|int, mixed>
Return values
array<string|int, mixed> —The property names of all flashdata
getTempdata()
Returns either a single piece of tempdata, or all temp data currently in the session.
public
getTempdata([string $key = null ]) : mixed
Parameters
- $key : string = null
-
Session data key
Return values
mixed —Session data value or null if not found.
getTempKeys()
Retrieve the keys of all session data that have been marked as temporary data.
public
getTempKeys() : array<string|int, mixed>
Return values
array<string|int, mixed> —has()
Returns whether an index exists in the session array.
public
has(string $key) : bool
Parameters
- $key : string
-
Identifier of the session property we are interested in.
Return values
bool —keepFlashdata()
Keeps a single piece of flash data alive for one more request.
public
keepFlashdata(array<string|int, mixed>|string $key) : mixed
Parameters
- $key : array<string|int, mixed>|string
-
Property identifier or array of them
Return values
mixed —markAsFlashdata()
Mark a session property or properties as flashdata.
public
markAsFlashdata(array<string|int, mixed>|string $key) : bool
Parameters
- $key : array<string|int, mixed>|string
-
Property identifier or array of them
Return values
bool —False if any of the properties are not already set
markAsTempdata()
Mark one of more pieces of data as being temporary, meaning that it has a set lifespan within the session.
public
markAsTempdata(string|array<string|int, mixed> $key[, int $ttl = 300 ]) : bool
Parameters
- $key : string|array<string|int, mixed>
-
Property identifier or array of them
- $ttl : int = 300
-
Time to live, in seconds
Return values
bool —False if any of the properties were not set
push()
Push new value onto session value that is array.
public
push(string $key, array<string|int, mixed> $data) : void
Parameters
- $key : string
-
Identifier of the session property we are interested in.
- $data : array<string|int, mixed>
-
value to be pushed to existing session key.
Return values
void —regenerate()
Regenerates the session ID.
public
regenerate([bool $destroy = false ]) : mixed
Parameters
- $destroy : bool = false
-
Should old session data be destroyed?
Return values
mixed —remove()
Remove one or more session properties.
public
remove(string|array<string|int, mixed> $key) : mixed
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
- $key : string|array<string|int, mixed>
-
Identifier of the session property or properties to remove.
Return values
mixed —removeTempdata()
Removes a single piece of temporary data from the session.
public
removeTempdata(string $key) : mixed
Parameters
- $key : string
-
Session data key
Return values
mixed —set()
Sets user data into the session.
public
set(string|array<string|int, mixed> $data[, mixed $value = null ]) : mixed
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
- $data : string|array<string|int, mixed>
-
Property name or associative array of properties
- $value : mixed = null
-
Property value if single key provided
Return values
mixed —setFlashdata()
Sets data into the session that will only last for a single request.
public
setFlashdata(array<string|int, mixed>|string $data[, string|array<string|int, mixed> $value = null ]) : mixed
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
- $data : array<string|int, mixed>|string
-
Property identifier or associative array of properties
- $value : string|array<string|int, mixed> = null
-
Property value if $data is a scalar
Return values
mixed —setLogger()
Sets a logger.
public
setLogger(LoggerInterface $logger) : mixed
Parameters
- $logger : LoggerInterface
Return values
mixed —setTempdata()
Sets new data into the session, and marks it as temporary data with a set lifespan.
public
setTempdata(string|array<string|int, mixed> $data[, null $value = null ][, int $ttl = 300 ]) : mixed
Parameters
- $data : string|array<string|int, mixed>
-
Session data key or associative array of items
- $value : null = null
-
Value to store
- $ttl : int = 300
-
Time-to-live in seconds
Return values
mixed —start()
Initialize the session container and starts up the session.
public
start() : mixed
Return values
mixed —stop()
Does a full stop of the session:
public
stop() : mixed
- destroys the session
- unsets the session id
- destroys the session cookie
Return values
mixed —unmarkFlashdata()
Unmark data in the session as flashdata.
public
unmarkFlashdata(mixed $key) : mixed
Parameters
- $key : mixed
-
Property identifier or array of them
Return values
mixed —unmarkTempdata()
Unmarks temporary data in the session, effectively removing its lifespan and allowing it to live as long as the session does.
public
unmarkTempdata(string|array<string|int, mixed> $key) : mixed
Parameters
- $key : string|array<string|int, mixed>
-
Property identifier or array of them
Return values
mixed —configure()
Configuration.
protected
configure() : mixed
Handle input binds and configuration defaults.
Return values
mixed —configureSidLength()
Configure session ID length
protected
configureSidLength() : void
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
Return values
void —initVars()
Handle temporary variables
protected
initVars() : mixed
Clears old "flash" data, marks the new one for deletion and handles "temp" data deletion.
Return values
mixed —setCookie()
Takes care of setting the cookie on the client side.
protected
setCookie() : mixed
Tags
Return values
mixed —setSaveHandler()
Sets the driver as the session handler in PHP.
protected
setSaveHandler() : mixed
Extracted for easier testing.
Return values
mixed —startSession()
Starts the session.
protected
startSession() : mixed
Extracted for testing reasons.