0 follower

Final Class Yiisoft\Session\Session

InheritanceYiisoft\Session\Session
ImplementsYiisoft\Session\SessionInterface

Session provides session data management and the related configurations.

Psalm Types

Name Value
SessionOptions array{name?: string}&array<string, mixed>

Constants

Hide inherited 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

Hide inherited methods

__construct() public method

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);
}

            
all() public method

public array all ( )

                public function all(): array
{
    if ($this->getId() === null) {
        return [];
    }
    $this->open();
    /** @var array $_SESSION */
    return $_SESSION;
}

            
clear() public method

public void clear ( )

                public function clear(): void
{
    if ($this->getId() !== null) {
        $this->open();
        $_SESSION = [];
    }
}

            
close() public method

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);
        }
    }
}

            
destroy() public method

public void destroy ( )

                public function destroy(): void
{
    if ($this->isActive()) {
        session_destroy();
        $this->sessionId = null;
    }
}

            
discard() public method

public void discard ( )

                public function discard(): void
{
    if ($this->isActive()) {
        session_abort();
    }
}

            
get() public method

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;
}

            
getCookieParameters() public method

public array getCookieParameters ( )

                public function getCookieParameters(): array
{
    return session_get_cookie_params();
}

            
getId() public method

public string|null getId ( )

                public function getId(): ?string
{
    return $this->sessionId === '' ? null : $this->sessionId;
}

            
getName() public method

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();
}

            
has() public method

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]);
}

            
isActive() public method

public boolean isActive ( )

                public function isActive(): bool
{
    return session_status() === PHP_SESSION_ACTIVE;
}

            
open() public method

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);
    }
}

            
pull() public method

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;
}

            
regenerateId() public method

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);
        }
    }
}

            
remove() public method

public void remove ( string $key )
$key string

                public function remove(string $key): void
{
    if ($this->getId() === null) {
        return;
    }
    $this->open();
    unset($_SESSION[$key]);
}

            
set() public method

public void set ( string $key, mixed $value )
$key string
$value mixed

                public function set(string $key, $value): void
{
    $this->open();
    $_SESSION[$key] = $value;
}

            
setId() public method

public void setId ( string $sessionId )
$sessionId string

                public function setId(string $sessionId): void
{
    $this->sessionId = $sessionId;
}