PHP code example of seba1rx / tabmanager

1. Go to this page and download the library: Download seba1rx/tabmanager library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

seba1rx / tabmanager example snippets



Seba1rx\TabManager\TabManager;

$tabManager = new TabManager();

// Write tab-specific data
$tabManager->set('user_step', 3);
$tabManager->set('draft_id', 42);

// Read it back (from the same tab only)
$step = $tabManager->get('user_step'); // 3

 // heartbeat-tab.php
_TABMANAGER_TABID'] ?? null;
if ($tabId) {
    $tm = new Seba1rx\TabManager\TabManager();
    $tm->touchTab($tabId);
}
http_response_code(204);

interface SessionStoreInterface
{
    public function start(): void;           // start if not already active (idempotent)
    public function isActive(): bool;        // whether the session is currently active
    public function get(string $key, mixed $default = null): mixed;
    public function set(string $key, mixed $value): void;
    public function has(string $key): bool;
    public function remove(string $key): void;
}

$tm = new TabManager(); // uses PhpSessionStore → calls session_start() if needed

use Seba1rx\TabManager\Contracts\SessionStoreInterface;
use Seba1rx\TabManager\TabManager;

class MySessionStore implements SessionStoreInterface
{
    public function start(): void  { /* session already started by your framework */ }
    public function isActive(): bool { return /* check your session driver */; }
    public function get(string $key, mixed $default = null): mixed { /* ... */ }
    public function set(string $key, mixed $value): void { /* ... */ }
    public function has(string $key): bool { /* ... */ }
    public function remove(string $key): void { /* ... */ }
}

$tm = new TabManager(new MySessionStore());

 // tab-status.php
TP_X_TABMANAGER_TABID'] ?? null;
$indexed = $tabId && Seba1rx\TabManager\TabManager::isValidTabId($tabId)
    ? (new Seba1rx\TabManager\TabManager())->isTabIndexed($tabId)
    : false;
header('Content-Type: application/json');
echo json_encode(['indexed' => $indexed, 'tab_id' => $tabId]);

$_SESSION['tabmanager']['tabs'] = [
    '<uuid>' => [
        'data'        => [],           // key-value store for this tab
        'is_active'   => true,
        'last_active' => 1234567890,   // unix timestamp, updated by heartbeat
    ],
];

define('TABMANAGER_DEBUG', true);

use Seba1rx\TabManager\Bridge\SessionAdminBridge;

$session = new App\MySession();
$session->setTabHandler(new SessionAdminBridge());
$session->autoCleanupTabs = 30; // optional: remove tabs inactive for > 30 s
$session->activateSession();    // configures session name/lifetime, then starts session

// After the JS client registers the tab:
$session->tabHandler->set('cart', ['apple' => 3]);
$cart  = $session->tabHandler->get('cart');
$ready = $session->tabHandler->isTabIndexed(); // false until JS fires
html
<script>
    window.TABMANAGER_HEARTBEAT_URL = '/heartbeat-tab.php';
</script>
html
<script>window.TABMANAGER_TAB_STATUS_URL = '/tab-status.php';</script>