PHP code example of compwright / php-session

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

    

compwright / php-session example snippets


$sessionFactory = new Compwright\PhpSession\Factory();

$manager = $sessionFactory->psr16Session(
    /**
     * @param Psr\SimpleCache\CacheInterface
     */
    $cache,

    /**
     * @param array|Compwright\PhpSession\Config
     */
    [
        'name' => 'my_app',
        'sid_length' => 48,
        'sid_bits_per_character' => 5,
    ]
);

// Start the session
$manager->id($sid); // Read $sid from request
$started = $manager->start();
if ($started === false) {
    throw new RuntimeException("The session failed to start");
}

// Read/write the current session
$session = $manager->getCurrentSession();
$session["foo"] = "bar";
unset($session["bar"]);

// Save and close the session
$ended = $manager->write_close();
if ($ended === false) {
    throw new RuntimeException("The session failed to close properly, data may have been lost");
}