PHP code example of solophp / session

1. Go to this page and download the library: Download solophp/session 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/ */

    

solophp / session example snippets


use Solo\Session;

// Create session with default secure settings
$session = new Session();

// Store data
$session->set('user', $userData);

// Get data
$userData = $session->get('user');

// Check if data exists
if ($session->has('user')) {
    // ...
}

// Remove data
$session->unset('user');

// Clear all data
$session->clear();

// Completely destroy session
$session->destroy();

$session = new Session(
    lifetime: 3600,          // Cookie lifetime in seconds (0 = until browser closes)
    secure: true,            // Require HTTPS
    httpOnly: true,          // Prevent JavaScript access
    sameSite: 'Strict',      // CSRF protection (Strict|Lax|None)
    path: '/',               // Cookie path
    domain: '',              // Cookie domain
    useStrictMode: true,     // Enable strict mode
    gcMaxlifetime: 86400,    // Session garbage collection lifetime
    useCookiesOnly: true,    // Prevent session ID in URLs
    timeout: 1800            // Session timeout in seconds
);

// Check if session has expired
if ($session->isExpired()) {
    // Handle expired session
}

// Get last activity timestamp
$lastActivity = $session->getLastActivity();

// Get value with default fallback
$value = $session->get('key', 'default');

// Set value
$session->set('key', 'value');

// Check existence
$exists = $session->has('key');

// Remove specific key
$session->unset('key');

// Get all session data
$allData = $session->all();

// Clear all data
$session->clear();

// Regenerate session ID
$session->regenerateId();

// Destroy session completely
$session->destroy();

// Get current session ID
$id = $session->getCurrentId();

// Get session cookie name
$name = $session->getCookieName();

// Get session save path
$path = $session->getSavePath();

// Get session status
$status = $session->getStatus();

// Get configured timeout
$timeout = $session->getTimeout();