MockSession
extends Session
in package
Class MockSession
Provides a safe way to test the Session class itself, that doesn't interact with the session or cookies at all.
Table of Contents
Properties
- $cookies : array<int, Cookie>
- Holds our "cookie" data.
- $didRegenerate : mixed
- $session_id : string
- $config : Session
- Session Config
- $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.
- $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.
- $sessionTimeToUpdate : int
- How many seconds between CI regenerating the session ID.
- $sidRegexp : string
- sid regex expression
Methods
- __construct() : mixed
- Constructor.
- __get() : mixed
- Magic method to get session variables by simply calling $foo = $session->foo;
- __isset() : bool
- Magic method to check for session variables.
- __set() : void
- Magic method to set variables in the session by simply calling $session->foo = bar;
- close() : void
- Writes session data and close the current session.
- destroy() : void
- Destroys the current session.
- get() : mixed
- Get user data that has been set in the session.
- getFlashdata() : mixed
- Retrieve one or more items of flash data from the session.
- getFlashKeys() : array<int, string>
- 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<int, string>
- 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() : void
- Keeps a single piece of flash data alive for one more request.
- markAsFlashdata() : bool
- Mark a session property or properties as flashdata. This returns `false` if any of the properties were not already set.
- 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() : void
- Regenerates the session ID.
- remove() : void
- Remove one or more session properties.
- removeTempdata() : void
- Removes a single piece of temporary data from the session.
- set() : void
- Sets user data into the session.
- setFlashdata() : void
- Sets data into the session that will only last for a single request.
- setTempdata() : void
- Sets new data into the session, and marks it as temporary data with a set lifespan.
- start() : $this|null
- Initialize the session container and starts up the session.
- stop() : void
- Destroys the current session.
- unmarkFlashdata() : void
- Unmark data in the session as flashdata.
- unmarkTempdata() : void
- Unmarks temporary data in the session, effectively removing its lifespan and allowing it to live as long as the session does.
- configure() : void
- Configuration.
- configureSidLength() : void
- Configure session ID length
- initVars() : void
- Handle temporary variables
- setCookie() : void
- Takes care of setting the cookie on the client side.
- setSaveHandler() : void
- Sets the driver as the session handler in PHP.
- startSession() : void
- Starts the session.
- removeOldSessionCookie() : void
Properties
$cookies
Holds our "cookie" data.
public
array<int, Cookie>
$cookies
= []
$didRegenerate
public
mixed
$didRegenerate
= false
$session_id
public
string
$session_id
$config
Session Config
protected
Session
$config
$cookie
The session cookie instance.
protected
Cookie
$cookie
$cookieDomain
The domain name to use for cookies.
No longer used.
protected
string
$cookieDomain
= ''
Set to .your-domain.com for site-wide cookies.
$cookiePath
Path used for storing cookies.
No longer used.
protected
string
$cookiePath
= '/'
Typically will be a forward slash.
$cookieSameSite
Cookie SameSite setting as described in RFC6265 Must be 'None', 'Lax' or 'Strict'.
No longer used.
protected
string
$cookieSameSite
= \CodeIgniter\Cookie\Cookie::SAMESITE_LAX
$cookieSecure
Cookie will only be set if a secure HTTPS connection exists.
No longer used.
protected
bool
$cookieSecure
= false
$driver
Instance of the driver to use.
protected
SessionHandlerInterface
$driver
$sessionCookieName
The session cookie name, must contain only [0-9a-z_-] characters.
Use $this->config->cookieName.
protected
string
$sessionCookieName
= 'ci_session'
$sessionDriverName
The storage driver to use: files, database, redis, memcached
Use $this->config->driver.
protected
string
$sessionDriverName
$sessionExpiration
The number of SECONDS you want the session to last.
Use $this->config->expiration.
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.
Use $this->config->matchIP.
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.
Use $this->config->regenerateDestroy.
protected
bool
$sessionRegenerateDestroy
= false
$sessionSavePath
The location to save sessions to, driver dependent.
Use $this->config->savePath.
protected
string
$sessionSavePath
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.
Tags
$sessionTimeToUpdate
How many seconds between CI regenerating the session ID.
Use $this->config->timeToUpdate.
protected
int
$sessionTimeToUpdate
= 300
$sidRegexp
sid regex expression
protected
string
$sidRegexp
Methods
__construct()
Constructor.
public
__construct(SessionHandlerInterface $driver, Session $config) : mixed
Extract configuration settings and save them here.
Parameters
- $driver : SessionHandlerInterface
- $config : Session
__get()
Magic method to get session variables by simply calling $foo = $session->foo;
public
__get(string $key) : mixed
Parameters
- $key : string
-
Identifier of the session property to remove.
__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, mixed $value) : void
Parameters
- $key : string
-
Identifier of the session property to set.
- $value : mixed
close()
Writes session data and close the current session.
public
close() : void
destroy()
Destroys the current session.
public
destroy() : void
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
getFlashdata()
Retrieve one or more items of flash data from the session.
public
getFlashdata([string|null $key = null ]) : mixed
If the item key is null, return all flashdata.
Parameters
- $key : string|null = null
-
Property identifier
getFlashKeys()
Retrieve all of the keys for session data marked as flashdata.
public
getFlashKeys() : array<int, string>
Return values
array<int, string>getTempdata()
Returns either a single piece of tempdata, or all temp data currently in the session.
public
getTempdata([string|null $key = null ]) : mixed
Parameters
- $key : string|null = null
-
Session data key
getTempKeys()
Retrieve the keys of all session data that have been marked as temporary data.
public
getTempKeys() : array<int, string>
Return values
array<int, string>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
boolkeepFlashdata()
Keeps a single piece of flash data alive for one more request.
public
keepFlashdata(array<int, string>|string $key) : void
Parameters
- $key : array<int, string>|string
-
Property identifier or array of them
markAsFlashdata()
Mark a session property or properties as flashdata. This returns `false` if any of the properties were not already set.
public
markAsFlashdata(array<int, string>|string $key) : bool
Parameters
- $key : array<int, string>|string
-
Property identifier or array of them
Return values
boolmarkAsTempdata()
Mark one of more pieces of data as being temporary, meaning that it has a set lifespan within the session.
public
markAsTempdata(array<string, mixed>|array<int, string>|string $key[, int $ttl = 300 ]) : bool
Returns false
if any of the properties were not set.
Parameters
- $key : array<string, mixed>|array<int, string>|string
-
Property identifier or array of them
- $ttl : int = 300
-
Time to live, in seconds
Return values
boolpush()
Push new value onto session value that is array.
public
push(string $key, array<string, mixed> $data) : void
Parameters
- $key : string
-
Identifier of the session property we are interested in.
- $data : array<string, mixed>
-
value to be pushed to existing session key.
regenerate()
Regenerates the session ID.
public
regenerate([bool $destroy = false ]) : void
Parameters
- $destroy : bool = false
-
Should old session data be destroyed?
remove()
Remove one or more session properties.
public
remove(array<int, string>|string $key) : void
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 : array<int, string>|string
-
Identifier of the session property or properties to remove.
removeTempdata()
Removes a single piece of temporary data from the session.
public
removeTempdata(string $key) : void
Parameters
- $key : string
-
Session data key
set()
Sets user data into the session.
public
set(array<string, mixed>|array<int, string>|string $data[, mixed $value = null ]) : void
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 : array<string, mixed>|array<int, string>|string
-
Property name or associative array of properties
- $value : mixed = null
-
Property value if single key provided
setFlashdata()
Sets data into the session that will only last for a single request.
public
setFlashdata(array<string, mixed>|string $data[, mixed $value = null ]) : void
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, mixed>|string
-
Property identifier or associative array of properties
- $value : mixed = null
-
Property value if $data is a scalar
setTempdata()
Sets new data into the session, and marks it as temporary data with a set lifespan.
public
setTempdata(array<string, mixed>|array<int, string>|string $data[, mixed $value = null ][, int $ttl = 300 ]) : void
Parameters
- $data : array<string, mixed>|array<int, string>|string
-
Session data key or associative array of items
- $value : mixed = null
-
Value to store
- $ttl : int = 300
-
Time-to-live in seconds
start()
Initialize the session container and starts up the session.
public
start() : $this|null
Return values
$this|nullstop()
Destroys the current session.
public
stop() : void
Use destroy() instead.
unmarkFlashdata()
Unmark data in the session as flashdata.
public
unmarkFlashdata(array<int, string>|string $key) : void
Parameters
- $key : array<int, string>|string
-
Property identifier or array of them
unmarkTempdata()
Unmarks temporary data in the session, effectively removing its lifespan and allowing it to live as long as the session does.
public
unmarkTempdata(array<int, string>|string $key) : void
Parameters
- $key : array<int, string>|string
-
Property identifier or array of them
configure()
Configuration.
protected
configure() : void
Handle input binds and configuration defaults.
configureSidLength()
Configure session ID length
protected
configureSidLength() : void
To make life easier, we force the PHP defaults. Because PHP9 forces them. See https://wiki.php.net/rfc/deprecations_php_8_4#sessionsid_length_and_sessionsid_bits_per_character
initVars()
Handle temporary variables
protected
initVars() : void
Clears old "flash" data, marks the new one for deletion and handles "temp" data deletion.
setCookie()
Takes care of setting the cookie on the client side.
protected
setCookie() : void
Extracted for testing reasons.
setSaveHandler()
Sets the driver as the session handler in PHP.
protected
setSaveHandler() : void
Extracted for easier testing.
startSession()
Starts the session.
protected
startSession() : void
Extracted for testing reasons.
removeOldSessionCookie()
private
removeOldSessionCookie() : void