Skip to content

Internals Session SessionHandler

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

session\SessionHandler

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/session/SessionHandler.php · Snapshot 8411f4287706.

Session handling reads database settings, starts PHP sessions, and maintains session metadata. SessionHandler() is an explicit method, not a PHP constructor.

Imports and dependencies

use setting\Config;

SessionHandler

Declaration and architectural role

class SessionHandler

Constants, enum cases, traits and properties

Declarations below preserve visibility, type, default and modifiers exactly. A typed property without an initializer remains uninitialized until its constructor or another method assigns it. Mutable fields belong to this instance unless marked static; readonly fields can be initialized once. Constructor-promoted properties are also listed below.

Member Declaration Purpose or source contract
$time private string $time; Used by SessionHandler().

Method index

Method Signature
SessionHandler public function SessionHandler(): bool
sessionClearing protected function sessionClearing(): void
sessionDumper public function sessionDumper(): void

SessionHandler

Source line 22

public function SessionHandler(): bool

Initializes and manages session handling.

  • Starts the session if it’s not already active.
  • Configures session cookie parameters such as SameSite, Secure, and HttpOnly based on configuration.
  • Tracks session start time and manages session expiration and ID regeneration.
  • Optionally binds session to a client IP address to prevent session hijacking.
  • Clears the session when necessary (e.g., expiration, IP mismatch).

@return bool True if the session is successfully handled, false if session clearing occurs due to IP mismatch.

State writes: $this->time.

Return expressions (including local callbacks): true.

External effects include session_start, session_regenerate_id.

Implementation, validation guards and interaction order
/**
     * Initializes and manages session handling.
     *
     * - Starts the session if it’s not already active.
     * - Configures session cookie parameters such as SameSite, Secure, and HttpOnly based on configuration.
     * - Tracks session start time and manages session expiration and ID regeneration.
     * - Optionally binds session to a client IP address to prevent session hijacking.
     * - Clears the session when necessary (e.g., expiration, IP mismatch).
     *
     * @return bool True if the session is successfully handled, false if session clearing occurs due to IP mismatch.
     */
    public function SessionHandler(): bool
    {
        $this->time = time();

        if (session_status() !== PHP_SESSION_ACTIVE) {
            $sameSite = Config::getConfig("AWT", "Session SameSite")->getValue();
            $sameSite = ($sameSite == 'true') ? "Strict" : "None";

            $secure = Config::getConfig("AWT", "Session HTTPS Only")->getValue() == 'true';
            $httpOnly = Config::getConfig("AWT", "Session HTTP Only")->getValue() == 'true';

            session_set_cookie_params([
                "SameSite" => $sameSite,
                "Secure" => $secure,
                "HttpOnly" => $httpOnly
            ]);

            session_start();
        }

        $_SESSION['sessionInfo']['started'] = $_SESSION['sessionInfo']['started'] ?? $this->time;

        if (isset($_SESSION['sessionInfo']['expires'])) {
            if ($_SESSION['sessionInfo']['expires'] < $this->time) {
                $this->sessionClearing();
            } else {
                $_SESSION['sessionInfo']['expires'] += Config::getConfig("AWT", "Session ID Regeneration Time")->getValue();
            }
        }

        if (isset($_SESSION['sessionInfo']['regenerate_id'])) {
            if ($this->time - $_SESSION['sessionInfo']['regenerate_id'] > 10) {
                $_SESSION['sessionInfo']['regenerate_id'] = $this->time;
                session_regenerate_id(true);
            }
        } else {
            $_SESSION['sessionInfo']['regenerate_id'] = $this->time;
        }

        return true;
    }

sessionClearing

Source line 64

protected function sessionClearing(): void

External effects include session_destroy.

Implementation, validation guards and interaction order
protected function sessionClearing(): void
    {
        unset($_SESSION);
        unset($_COOKIE['LAST_IP']);
        session_destroy();

    }

sessionDumper

Source line 72

public function sessionDumper(): void

Computes or delegates the operation shown below; no explicit receiver property assignment or throw occurs in this method.

Implementation, validation guards and interaction order
public function sessionDumper(): void
    {
        var_dump($_SESSION);
    }

Related types

Clone this wiki locally