PHP code example of natilosir / session

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

    

natilosir / session example snippets


Session::set('username', 'John Doe');

$username = Session::get('username');
echo 'Username: ' . $username; // Outputs: Username: John Doe

Session::delete('username');

Session::set('user_id', 1);
Session::clear(); // Clears all session variables

Session::destroy();

$all = Session::list();
print_r($all); // Outputs all session variables

// Set a session variable
Session::set('username', 'John Doe');

// Get the session variable
$username = Session::get('username');
echo 'Username: ' . $username; // Outputs: Username: John Doe

// Delete the session variable
Session::delete('username');

// Clear all session variables
Session::clear(); // Clears all session variables

// Check if all session variables are cleared
$userId = Session::get('user_id');
echo 'User ID after clearing: ' . ($userId ?? 'Not set'); // Outputs: User ID after clearing: Not set

// Destroy the session
Session::destroy();

// List all session variables
$all = Session::list();
print_r($all); // Outputs all session variables