Final Class Yiisoft\Session\Session
| Inheritance | Yiisoft\Session\Session |
|---|---|
| Implements | Yiisoft\Session\SessionInterface |
Session provides session data management and the related configurations.
Public Methods
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| DEFAULT_OPTIONS | [ 'cookie_secure' => 1, 'use_only_cookies' => 1, 'cookie_httponly' => 1, 'use_strict_mode' => 1, 'cookie_samesite' => 'Lax', ] | Yiisoft\Session\Session |
Method Details
| public mixed __construct ( array $options = [], SessionHandlerInterface|null $handler = null ) | ||
| $options | array |
Session options. See {@link https://www.php.net/manual/en/session.configuration.php}. |
| $handler | SessionHandlerInterface|null |
Session handler. If not specified, default PHP handler is used. |
public function __construct(array $options = [], ?SessionHandlerInterface $handler = null)
{
if ($handler !== null) {
session_set_save_handler($handler, true);
}
// We set cookies using SessionMiddleware.
$options['use_cookies'] = 0;
// Prevent PHP to send headers.
unset($options['cache_limiter']);
$this->options = array_merge(self::DEFAULT_OPTIONS, $options);
}
| public array all ( ) |
public function all(): array
{
if ($this->getId() === null) {
return [];
}
$this->open();
/** @var array $_SESSION */
return $_SESSION;
}
| public void clear ( ) |
public function clear(): void
{
if ($this->getId() !== null) {
$this->open();
$_SESSION = [];
}
}
| public void close ( ) |
public function close(): void
{
if ($this->isActive()) {
try {
session_write_close();
} catch (Throwable $e) {
throw new SessionException('Unable to close session.', (int) $e->getCode(), $e);
}
}
}
| public void destroy ( ) |
public function destroy(): void
{
if ($this->isActive()) {
session_destroy();
$this->sessionId = null;
}
}
| public void discard ( ) |
public function discard(): void
{
if ($this->isActive()) {
session_abort();
}
}
| public mixed get ( string $key, mixed $default = null ) | ||
| $key | string | |
| $default | mixed | |
public function get(string $key, $default = null)
{
if ($this->getId() === null) {
return $default;
}
$this->open();
return $_SESSION[$key] ?? $default;
}
| public array getCookieParameters ( ) |
public function getCookieParameters(): array
{
return session_get_cookie_params();
}
| public string|null getId ( ) |
public function getId(): ?string
{
return $this->sessionId === '' ? null : $this->sessionId;
}
| public string getName ( ) |
public function getName(): string
{
if ($this->isActive()) {
/**
* @var string Without `name` parameter `session_name()` always returns string.
*/
return session_name();
}
/**
* @var string Without `name` parameter `session_name()` always returns string.
*/
return $this->options['name'] ?? session_name();
}
| public boolean has ( string $key ) | ||
| $key | string | |
public function has(string $key): bool
{
if ($this->getId() === null) {
return false;
}
$this->open();
return isset($_SESSION[$key]);
}
| public boolean isActive ( ) |
public function isActive(): bool
{
return session_status() === PHP_SESSION_ACTIVE;
}
| public void open ( ) | ||
| throws | Yiisoft\Session\SessionException |
When start session is failed. |
|---|---|---|
public function open(): void
{
if ($this->isActive()) {
return;
}
if ($this->sessionId !== null) {
session_id($this->sessionId);
}
try {
session_start($this->options);
/**
* @var string Without `id` parameter `session_id()` always returns string.
*/
$this->sessionId = session_id();
} catch (Throwable $e) {
throw new SessionException('Failed to start session.', (int)$e->getCode(), $e);
}
}
| public mixed pull ( string $key, mixed $default = null ) | ||
| $key | string | |
| $default | mixed | |
public function pull(string $key, $default = null)
{
$value = $this->get($key, $default);
$this->remove($key);
return $value;
}
| public void regenerateId ( ) |
public function regenerateId(): void
{
if ($this->isActive()) {
try {
if (session_regenerate_id(true)) {
/**
* @var string Without `id` parameter `session_id()` always returns string.
*/
$this->sessionId = session_id();
}
} catch (Throwable $e) {
throw new SessionException('Failed to regenerate ID.', (int)$e->getCode(), $e);
}
}
}
| public void remove ( string $key ) | ||
| $key | string | |
public function remove(string $key): void
{
if ($this->getId() === null) {
return;
}
$this->open();
unset($_SESSION[$key]);
}
| public void set ( string $key, mixed $value ) | ||
| $key | string | |
| $value | mixed | |
public function set(string $key, $value): void
{
$this->open();
$_SESSION[$key] = $value;
}
Signup or Login in order to comment.