PHP Class Cake\Network\Session

CakePHP abstracts the handling of sessions. There are several convenient methods to access session information. This class is the implementation of those methods. They are mostly used by the Session Component.
Datei anzeigen Open project: cakephp/cakephp Class Usage Examples

Protected Properties

Property Type Description
$_engine SessionHandlerInterface The Session handler instance used as an engine for persisting the session data.
$_isCLI boolean Whether this session is running under a CLI environment
$_lifetime integer The time in seconds the session will be valid for
$_started boolean Indicates whether the sessions has already started

Public Methods

Method Description
__construct ( array $config = [] ) Constructor.
check ( string | null $name = null ) : boolean Returns true if given variable name is set in session.
clear ( boolean $renew = false ) : void Clears the session.
consume ( string $name ) : mixed Reads and deletes a variable from session.
create ( array $sessionConfig = [] ) : Session Returns a new instance of a session after building a configuration bundle for it.
delete ( string $name ) : void Removes a variable from session.
destroy ( ) : void Helper method to destroy invalid sessions.
engine ( string | SessionHandlerInterface | null $class = null, array $options = [] ) : SessionHandlerInterface | null Sets the session handler instance to use for this session.
id ( string | null $id = null ) : string Returns the session id.
options ( array $options ) : void Calls ini_set for each of the keys in $options and set them to the respective value in the passed array.
read ( string | null $name = null ) : string | null Returns given session variable, or all of them, if no parameters given.
renew ( ) : void Restarts this session.
start ( ) : boolean Starts the Session.
started ( ) : boolean Determine if Session has already been started.
write ( string | array $name, mixed $value = null ) : void Writes value to given session variable name.

Protected Methods

Method Description
_defaultConfig ( string $name ) : boolean | array Get one of the prebaked default session configurations.
_hasSession ( ) : boolean Returns whether a session exists
_overwrite ( array &$old, array $new ) : void Used to write new data to _SESSION, since PHP doesn't like us setting the _SESSION var itself.
_timedOut ( ) : boolean Returns true if the session is no longer valid because the last time it was accessed was after the configured timeout.

Method Details

__construct() public method

### Configuration: - timeout: The time in minutes the session should be valid for. - cookiePath: The url path for which session cookie is set. Maps to the session.cookie_path php.ini config. Defaults to base path of app. - ini: A list of php.ini directives to change before the session start. - handler: An array containing at least the class key. To be used as the session engine for persisting data. The rest of the keys in the array will be passed as the configuration array for the engine. You can set the class key to an already instantiated session handler object.
public __construct ( array $config = [] )
$config array The Configuration to apply to this session object

_defaultConfig() protected static method

Get one of the prebaked default session configurations.
protected static _defaultConfig ( string $name ) : boolean | array
$name string Config name.
return boolean | array

_hasSession() protected method

Returns whether a session exists
protected _hasSession ( ) : boolean
return boolean

_overwrite() protected method

Used to write new data to _SESSION, since PHP doesn't like us setting the _SESSION var itself.
protected _overwrite ( array &$old, array $new ) : void
$old array Set of old variables => values
$new array New set of variable => value
return void

_timedOut() protected method

Returns true if the session is no longer valid because the last time it was accessed was after the configured timeout.
protected _timedOut ( ) : boolean
return boolean

check() public method

Returns true if given variable name is set in session.
public check ( string | null $name = null ) : boolean
$name string | null Variable name to check for
return boolean True if variable is there

clear() public method

Optionally it also clears the session id and renews the session.
public clear ( boolean $renew = false ) : void
$renew boolean If session should be renewed, as well. Defaults to false.
return void

consume() public method

Reads and deletes a variable from session.
public consume ( string $name ) : mixed
$name string The key to read and remove (or a path as sent to Hash.extract).
return mixed The value of the session variable, null if session not available, session not started, or provided name not found in the session.

create() public static method

This function allows an options array which will be used for configuring the session and the handler to be used. The most important key in the configuration array is defaults, which indicates the set of configurations to inherit from, the possible defaults are: - php: just use session as configured in php.ini - cache: Use the CakePHP caching system as an storage for the session, you will need to pass the config key with the name of an already configured Cache engine. - database: Use the CakePHP ORM to persist and manage sessions. By default this requires a table in your database named sessions or a model key in the configuration to indicate which Table object to use. - cake: Use files for storing the sessions, but let CakePHP manage them and decide where to store them. The full list of options follows: - defaults: either 'php', 'database', 'cache' or 'cake' as explained above. - handler: An array containing the handler configuration - ini: A list of php.ini directives to set before the session starts. - timeout: The time in minutes the session should stay active
See also: Cake\Network\Session::__construct()
public static create ( array $sessionConfig = [] ) : Session
$sessionConfig array Session config.
return Session

delete() public method

Removes a variable from session.
public delete ( string $name ) : void
$name string Session variable to remove
return void

destroy() public method

Helper method to destroy invalid sessions.
public destroy ( ) : void
return void

engine() public method

If a string is passed for the first argument, it will be treated as the class name and the second argument will be passed as the first argument in the constructor. If an instance of a SessionHandlerInterface is provided as the first argument, the handler will be set to it. If no arguments are passed it will return the currently configured handler instance or null if none exists.
public engine ( string | SessionHandlerInterface | null $class = null, array $options = [] ) : SessionHandlerInterface | null
$class string | SessionHandlerInterface | null The session handler to use
$options array the options to pass to the SessionHandler constructor
return SessionHandlerInterface | null

id() public method

Calling this method will not auto start the session. You might have to manually assert a started session. Passing an id into it, you can also replace the session id if the session has not already been started. Note that depending on the session handler, not all characters are allowed within the session id. For example, the file session handler only allows characters in the range a-z A-Z 0-9 , (comma) and - (minus).
public id ( string | null $id = null ) : string
$id string | null Id to replace the current session id
return string Session id

options() public method

### Example: $session->options(['session.use_cookies' => 1]);
public options ( array $options ) : void
$options array Ini options to set.
return void

read() public method

Returns given session variable, or all of them, if no parameters given.
public read ( string | null $name = null ) : string | null
$name string | null The name of the session variable (or a path as sent to Hash.extract)
return string | null The value of the session variable, null if session not available, session not started, or provided name not found in the session.

renew() public method

Restarts this session.
public renew ( ) : void
return void

start() public method

Starts the Session.
public start ( ) : boolean
return boolean True if session was started

started() public method

Determine if Session has already been started.
public started ( ) : boolean
return boolean True if session has been started.

write() public method

Writes value to given session variable name.
public write ( string | array $name, mixed $value = null ) : void
$name string | array Name of variable
$value mixed Value to write
return void

Property Details

$_engine protected_oe property

The Session handler instance used as an engine for persisting the session data.
protected SessionHandlerInterface $_engine
return SessionHandlerInterface

$_isCLI protected_oe property

Whether this session is running under a CLI environment
protected bool $_isCLI
return boolean

$_lifetime protected_oe property

The time in seconds the session will be valid for
protected int $_lifetime
return integer

$_started protected_oe property

Indicates whether the sessions has already started
protected bool $_started
return boolean