This class is a wrapper for the native PHP session functions. It provides several defaults for the most common session configuration via external handlers and helps with using session in cli without any warnings.
Sessions can be created from the defaults using Session::create()
or you can get an instance of a new session by just instantiating this class and passing the complete options you want to use.
When specific options are omitted, this class will take its defaults from the configuration values from the session.*
directives in php.ini. This class will also alter such directives when configuration values are provided.
$_engine
protected SessionHandlerInterface
$_isCLI
protected boolean
$_lifetime
protected integer
$_started
protected boolean
Returns true if the session is no longer valid because the last time it was accessed was after the configured timeout.
Returns a new instance of a session after building a configuration bundle for it. 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:
Sets the session handler instance to use for this session. 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.
Returns the session id. Calling this method will not auto start the session. You might have to manually assert a started session.
Calls ini_set for each of the keys in $options
and set them to the respective value in the passed array.
__construct( array $config [] )
Constructor.
session.cookie_path
php.ini config. Defaults to base path of app.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.$config
optional [] _defaultConfig( string $name )
Get one of the prebaked default session configurations.
$name
_overwrite( array $old , array $new )
Used to write new data to _SESSION, since PHP doesn't like us setting the _SESSION var itself.
$old
$new
_timedOut( )
Returns true if the session is no longer valid because the last time it was accessed was after the configured timeout.
check( string|null $name null )
Returns true if given variable name is set in session.
$name
optional null clear( boolean $renew false )
Clears the session.
Optionally it also clears the session id and renews the session.
$renew
optional false consume( string $name )
Reads and deletes a variable from session.
$name
The value of the session variable, null if session not available, session not started, or provided name not found in the session.
create( array $sessionConfig [] )
Returns a new instance of a session after building a configuration bundle for it. 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:
config
key with the name of an already configured Cache engine.sessions
or a model
key in the configuration to indicate which Table object to use.The full list of options follows:
$sessionConfig
optional [] Cake\Network\Session
delete( string $name )
Removes a variable from session.
$name
engine( string|SessionHandlerInterface|null $class null , array $options [] )
Sets the session handler instance to use for this session. 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.
$class
optional null $options
optional [] id( string|null $id null )
Returns the session id. 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).
$id
optional null options( array $options )
Calls ini_set for each of the keys in $options
and set them to the respective value in the passed array.
$session->options(['session.use_cookies' => 1]);
$options
read( string|null $name null )
Returns given session variable, or all of them, if no parameters given.
$name
optional null The value of the session variable, null if session not available, session not started, or provided name not found in the session.
start( )
Starts the Session.
started( )
Determine if Session has already been started.
write( string|array $name , mixed $value null )
Writes value to given session variable name.
$name
$value
optional null protected SessionHandlerInterface
The Session handler instance used as an engine for persisting the session data.
© 2005–2017 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/3.4/class-Cake.Network.Session.html