PHP code example of israel-nogueira / sky-session

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

    

israel-nogueira / sky-session example snippets




use IsraelNogueira\SkySession\Session;

// Criar instância
$session = new Session();

// Definir valores
$session->set('username', 'john_doe');
$session->set('user_data', [
    'email' => '[email protected]',
    'role' => 'admin'
]);

// Recuperar valores
echo $session->get('username'); // john_doe
$userData = $session->get('user_data');

// Verificar existência
if ($session->has('username')) {
    echo 'Usuário logado!';
}

// Remover valor
$session->unset('username');

// Obter todas as variáveis
$all = $session->all();



use IsraelNogueira\SkySession\Session;

$session = new Session();

// Set usando propriedade
$session->username = 'jane_doe';
$session->cart = ['item1', 'item2'];

// Get usando propriedade
echo $session->username; // jane_doe
print_r($session->cart);

// Isset
if (isset($session->username)) {
    echo 'Username existe!';
}

// Unset
unset($session->cart);



use IsraelNogueira\SkySession\Session;

// Set
Session::username('admin');
Session::preferences(['theme' => 'dark', 'lang' => 'pt-BR']);

// Get
echo Session::username(); // admin
$prefs = Session::preferences();

// Unset (passando null)
Session::username(null);

// Múltiplos argumentos
Session::data('arg1', 'arg2', 'arg3'); // armazena como array



use IsraelNogueira\SkySession\Session;

$session = new Session([
    'name' => 'custom_session',
    'lifetime' => 7200,
    'secure' => true,
    'cookie_path' => '/',
    'cookie_domain' => '.example.com',
    'cookie_secure' => true,
    'cookie_samesite' => 'Strict',
    'crypt_key' => 'your_key',
    'crypt_iv' => 'your_iv'
]);



use IsraelNogueira\SkySession\Session;

// Primeira chamada cria a instância
$session1 = Session::getInstance(['secure' => true]);

// Próximas chamadas retornam a mesma instância
$session2 = Session::getInstance();

// $session1 === $session2 (true)



use IsraelNogueira\SkySession\Session;

$session = new Session();

// Arrays complexos
$session->set('cart', [
    'items' => [
        ['id' => 1, 'name' => 'Product A', 'qty' => 2],
        ['id' => 2, 'name' => 'Product B', 'qty' => 1]
    ],
    'total' => 150.00,
    'discount' => 10.00
]);

// Objetos (convertidos automaticamente para array)
$user = new stdClass();
$user->name = 'John';
$user->email = '[email protected]';
$session->set('user', $user);

// Recuperação mantém estrutura
$cart = $session->get('cart');
echo $cart['items'][0]['name']; // Product A



use IsraelNogueira\SkySession\Session;

$session = new Session();

// Regenerar ID (recomendado após login)
if ($loginSuccess) {
    $session->regenerateId();
    $session->set('authenticated', true);
}



use IsraelNogueira\SkySession\Session;

$session = new Session();

// Logout completo
$session->destroy();
// Remove todos os dados, cookies e destrói a sessão



use IsraelNogueira\SkySession\Session;

// Para desenvolvimento ou quando criptografia não é necessária
$session = new Session(['secure' => false]);

$session->set('debug', 'visible data');
// Dados ficam visíveis em $_SESSION

// Chamadas dinâmicas
Session::variableName($value); // Set
Session::variableName();        // Get
Session::variableName(null);    // Unset

// Métodos com prefixo __
Session::__set($key, $value);
Session::__get($key);
Session::__regenerateId();
Session::__destroy();

$session->property = $value;    // __set
$value = $session->property;    // __get
isset($session->property);      // __isset
unset($session->property);      // __unset
bash
# Gerar chave de criptografia
php -r "echo bin2hex(random_bytes(16));"

# Gerar IV
php -r "echo base64_encode(random_bytes(16));"
bash
# Windows
php vendor\bin\phpunit vendor\israel-nogueira\sky-session\tests\Unit\

# Linux/Mac
php vendor/bin/phpunit vendor/israel-nogueira/sky-session/tests/Unit/