phpdevcommunity/php-session

PHP Session is a PHP library that optimizes session management, utilizing PHP's native session handling mechanisms for enhanced security and efficiency.

1.0.1 2024-12-26 08:38 UTC

This package is auto-updated.

Last update: 2024-12-26 08:39:46 UTC


README

Introduction

The SessionStorage is a PHP library that provides a simple implementation of the SessionStorageInterface. It allows developers to work with PHP sessions in a convenient way, providing methods to get, set, check, and remove session data. This library requires PHP version 7.4 or higher.

Installation

Use Composer

Composer Require

composer require phpdevcommunity/php-session

Usage

To start using the SessionStorage, you need to create an instance of the NativeSessionStorage class. Here's an example of how to do it:

use PhpDevCommunity\Session\Storage\NativeSessionStorage;

// Create a new session storage instance

/**
 * Constructor for NativeSessionStorage class.
 *
 * @param array $options Options for session start.
 *                      Possible options:
 *                      - 'name': Session name
 *                      - 'lifetime': Session lifetime
 *                      - 'path': Session save path
 *                      - 'domain': Session domain
 *                      - 'secure': Set to true for secure session
 *                      - 'httponly': Set to true to only allow HTTP access
 * @throws \RuntimeException If session start fails.
 */
$sessionStorage = new NativeSessionStorage();

Use Cases

The SessionStorage library offers the following methods to interact with PHP sessions:

1. Check if a key exists in the session:

if ($sessionStorage->has('user_id')) {
    // Do something with the user_id
}

2. Get the value of a session key:

$userName = $sessionStorage->get('username', 'Guest');
// If the 'username' key exists in the session, $userName will be set to its value,
// otherwise, it will be set to 'Guest'.

3. Set a value in the session:

$userId = 123;
$sessionStorage->put('user_id', $userId);

4. Remove a key from the session:

$sessionStorage->remove('user_id');

5. Get all session data as an array:

$allData = $sessionStorage->all();

Interface Implementation

The NativeSessionStorage class implements the SessionStorageInterface, which extends the ArrayAccess interface. As a result, any class implementing the SessionStorageInterface should also implement the methods defined in the ArrayAccess interface. Here's how you can implement the SessionStorageInterface in a custom class:

use PhpDevCommunity\Session\Storage\SessionStorageInterface;

class MyCustomSessionStorage implements SessionStorageInterface
{
    private array $storage;

    public function __construct()
    {
        // Initialize your custom storage mechanism here
        // For example, you could use a database, Redis, or any other storage solution.
        // In this example, we will use an array as a simple custom storage mechanism.
        $this->storage = [];
    }

    public function get(string $key, $default = null)
    {
        return $this->storage[$key] ?? $default;
    }

    public function put(string $key, $value = null): void
    {
        $this->storage[$key] = $value;
    }

    public function all(): array
    {
        return $this->storage;
    }

    public function has(string $key): bool
    {
        return isset($this->storage[$key]);
    }

    public function remove(string $key): void
    {
        unset($this->storage[$key]);
    }

    // Implementing ArrayAccess methods

    public function offsetExists($offset): bool
    {
        return isset($this->storage[$offset]);
    }

    public function offsetGet($offset)
    {
        return $this->get($offset);
    }

    public function offsetSet($offset, $value): void
    {
        $this->put($offset, $value);
    }

    public function offsetUnset($offset): void
    {
        $this->remove($offset);
    }
}

In this example, we create a custom session storage class MyCustomSessionStorage, which implements the SessionStorageInterface. It uses a simple array to store session data, but you can replace this with any custom storage mechanism like a database, Redis, etc., depending on your specific use case.

Conclusion

The SessionStorage library simplifies working with PHP sessions by providing a clean and easy-to-use interface. It is well-suited for applications that need to manage session data efficiently.

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests to help improve the library.

License

This library is open-source software licensed under the MIT license.