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.
Afficher le fichier Open project: cakephp/cakephp Class Usage Examples

Protected Properties

Свойство 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

Méthodes publiques

Méthode 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.

Méthodes protégées

Méthode 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 méthode

### 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 méthode

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

_hasSession() protected méthode

Returns whether a session exists
protected _hasSession ( ) : boolean
Résultat boolean

_overwrite() protected méthode

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
Résultat void

_timedOut() protected méthode

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

check() public méthode

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
Résultat boolean True if variable is there

clear() public méthode

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.
Résultat void

consume() public méthode

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).
Résultat 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 méthode

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.
Résultat Session

delete() public méthode

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

destroy() public méthode

Helper method to destroy invalid sessions.
public destroy ( ) : void
Résultat void

engine() public méthode

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
Résultat SessionHandlerInterface | null

id() public méthode

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
Résultat string Session id

options() public méthode

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

read() public méthode

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)
Résultat 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 méthode

Restarts this session.
public renew ( ) : void
Résultat void

start() public méthode

Starts the Session.
public start ( ) : boolean
Résultat boolean True if session was started

started() public méthode

Determine if Session has already been started.
public started ( ) : boolean
Résultat boolean True if session has been started.

write() public méthode

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
Résultat void

Property Details

$_engine protected_oe property

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

$_isCLI protected_oe property

Whether this session is running under a CLI environment
protected bool $_isCLI
Résultat boolean

$_lifetime protected_oe property

The time in seconds the session will be valid for
protected int $_lifetime
Résultat integer

$_started protected_oe property

Indicates whether the sessions has already started
protected bool $_started
Résultat boolean