PHP Класс 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.
Показать файл Открыть проект Примеры использования класса

Защищенные свойства (Protected)

Свойство Тип Описание
$_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

Открытые методы

Метод Описание
__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.

Защищенные методы

Метод Описание
_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.

Описание методов

__construct() публичный метод

### 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() защищенный статический метод

Get one of the prebaked default session configurations.
protected static _defaultConfig ( string $name ) : boolean | array
$name string Config name.
Результат boolean | array

_hasSession() защищенный метод

Returns whether a session exists
protected _hasSession ( ) : boolean
Результат boolean

_overwrite() защищенный метод

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
Результат void

_timedOut() защищенный метод

Returns true if the session is no longer valid because the last time it was accessed was after the configured timeout.
protected _timedOut ( ) : boolean
Результат boolean

check() публичный метод

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
Результат boolean True if variable is there

clear() публичный метод

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.
Результат void

consume() публичный метод

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).
Результат mixed The value of the session variable, null if session not available, session not started, or provided name not found in the session.

create() публичный статический метод

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
См. также: Cake\Network\Session::__construct()
public static create ( array $sessionConfig = [] ) : Session
$sessionConfig array Session config.
Результат Session

delete() публичный метод

Removes a variable from session.
public delete ( string $name ) : void
$name string Session variable to remove
Результат void

destroy() публичный метод

Helper method to destroy invalid sessions.
public destroy ( ) : void
Результат void

engine() публичный метод

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
Результат SessionHandlerInterface | null

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).
public id ( string | null $id = null ) : string
$id string | null Id to replace the current session id
Результат string Session id

options() публичный метод

### Example: $session->options(['session.use_cookies' => 1]);
public options ( array $options ) : void
$options array Ini options to set.
Результат void

read() публичный метод

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)
Результат 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() публичный метод

Restarts this session.
public renew ( ) : void
Результат void

start() публичный метод

Starts the Session.
public start ( ) : boolean
Результат boolean True if session was started

started() публичный метод

Determine if Session has already been started.
public started ( ) : boolean
Результат boolean True if session has been started.

write() публичный метод

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
Результат void

Описание свойств

$_engine защищенное свойство

The Session handler instance used as an engine for persisting the session data.
protected SessionHandlerInterface $_engine
Результат SessionHandlerInterface

$_isCLI защищенное свойство

Whether this session is running under a CLI environment
protected bool $_isCLI
Результат boolean

$_lifetime защищенное свойство

The time in seconds the session will be valid for
protected int $_lifetime
Результат integer

$_started защищенное свойство

Indicates whether the sessions has already started
protected bool $_started
Результат boolean