{"id":18677231,"url":"https://github.com/lithemod/session","last_synced_at":"2026-02-06T12:37:31.775Z","repository":{"id":257807141,"uuid":"866472272","full_name":"lithemod/session","owner":"lithemod","description":"Manages user sessions, allowing you to store and retrieve both persistent and temporary information between requests, such as login data, preferences, and short-term data.","archived":false,"fork":false,"pushed_at":"2024-11-05T18:34:15.000Z","size":23,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-07-23T15:11:03.301Z","etag":null,"topics":["lithe","middleware","php","session"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lithemod.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-10-02T10:22:23.000Z","updated_at":"2024-11-05T18:34:10.000Z","dependencies_parsed_at":null,"dependency_job_id":"bca6d1aa-a528-474c-b940-e862834aa3cf","html_url":"https://github.com/lithemod/session","commit_stats":null,"previous_names":["lithemod/session"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/lithemod/session","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lithemod%2Fsession","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lithemod%2Fsession/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lithemod%2Fsession/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lithemod%2Fsession/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lithemod","download_url":"https://codeload.github.com/lithemod/session/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lithemod%2Fsession/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29160816,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-06T07:18:23.844Z","status":"ssl_error","status_checked_at":"2026-02-06T07:13:32.659Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["lithe","middleware","php","session"],"created_at":"2024-11-07T09:32:59.061Z","updated_at":"2026-02-06T12:37:31.744Z","avatar_url":"https://github.com/lithemod.png","language":"PHP","readme":"# Session\n\nThe session middleware in Lithe is responsible for managing user sessions, allowing you to store and retrieve persistent information between requests, such as login data and preferences.\n\n## 1. Installing the Session Middleware\n\nTo use the session middleware in Lithe, you need to install it via Composer. Composer is a dependency management tool for PHP.\n\n### Step by Step:\n\n1. **Open the terminal (command line)**.\n2. **Navigate to your project directory**. Use the `cd` command to change to the directory where your Lithe project is located. For example:\n   ```bash\n   cd /path/to/your/project\n   ```\n\n3. **Run the installation command**:\n   ```bash\n   composer require lithemod/session\n   ```\n\nThis command will download and install the session middleware and its dependencies.\n\n## 2. Configuring the Session Middleware\n\nAfter installing the middleware, you need to configure it in your Lithe application. This is done using the `use()` method.\n\n### Example Configuration:\n\n```php\nuse function Lithe\\Middleware\\Session\\session;\n\n// Add the middleware to the application\n$app-\u003euse(session());\n```\n\n### Configuration Parameters\n\nYou can configure the session middleware with some important parameters:\n\n- **lifetime**: Sets the session duration in seconds. The default is 2592000 (30 days).\n- **domain**: Sets the domain for which the session cookie is valid.\n- **secure**: Indicates whether the session cookie should only be sent over secure connections (HTTPS).\n- **httponly**: If it should only be accessible via HTTP requests.\n- **samesite**: Defines the SameSite attribute of the session cookie. It can be `'Lax'`, `'Strict'`, or `'None'`.\n- **path**: Defines the path where the session files will be stored.\n\n### Example Configuration with Parameters:\n\n```php\n$app-\u003euse(session([\n    'lifetime' =\u003e 3600, // 1 hour\n    'domain' =\u003e 'example.com',\n    'secure' =\u003e true, // Only over HTTPS\n    'httponly' =\u003e true, // Accessible only via HTTP\n    'samesite' =\u003e 'Strict', // SameSite policy\n    'path' =\u003e 'storage/framework/session', // Path to store sessions\n]));\n```\n\n## 3. Using Session Variables\n\nAfter configuration, you can access and manipulate session variables through the `Request` object. Let’s see how to do this through routes.\n\n### Route Examples\n\nHere are some examples of how to use session variables in Lithe routes.\n\n#### Setting a Session Variable\n\n```php\n$app-\u003eget('/set-user', function ($req, $res) {\n    $req-\u003esession-\u003eput('user', 'John Doe'); // Set the session variable\n    return $res-\u003esend('User set in the session!');\n});\n```\n\n#### Retrieving a Session Variable\n\n```php\n$app-\u003eget('/get-user', function ($req, $res) {\n    $user = $req-\u003esession-\u003eget('user', 'User not found'); // Retrieve the session variable\n    return $res-\u003esend('User: ' . $user);\n});\n```\n\n#### Removing a Session Variable\n\n```php\n$app-\u003eget('/remove-user', function ($req, $res) {\n    $req-\u003esession-\u003eforget('user'); // Remove the session variable\n    return $res-\u003esend('User removed from the session!');\n});\n```\n\n#### Destroying All Session Variables\n\n```php\n$app-\u003eget('/destroy-session', function ($req, $res) {\n    $req-\u003esession-\u003edestroy(); // Destroy all session variables\n    return $res-\u003esend('All session variables have been destroyed!');\n});\n```\n\n#### Checking if the Session is Active\n\n```php\n$app-\u003eget('/check-session', function ($req, $res) {\n    $isActive = $req-\u003esession-\u003eisActive(); // Check if the session is active\n    return $res-\u003esend('Session active: ' . ($isActive ? 'Yes' : 'No'));\n});\n```\n\n#### Regenerating the Session ID\n\n```php\n$app-\u003eget('/regenerate-session', function ($req, $res) {\n    $req-\u003esession-\u003eregenerate(); // Regenerate the session ID\n    return $res-\u003esend('Session ID regenerated!');\n});\n```\n\n#### Getting the Session ID\n\n```php\n$app-\u003eget('/session-id', function ($req, $res) {\n    $sessionId = $req-\u003esession-\u003egetId(); // Get the session ID\n    return $res-\u003esend('Session ID: ' . $sessionId);\n});\n```\n\n#### Setting a New Session ID\n\n```php\n$app-\u003eget('/set-session-id', function ($req, $res) {\n    $req-\u003esession-\u003esetId('newSessionId'); // Set a new ID for the session\n    return $res-\u003esend('New session ID set!');\n});\n```\n\n#### Getting All Session Variables\n\n```php\n$app-\u003eget('/all-session-data', function ($req, $res) {\n    $allSessionData = $req-\u003esession-\u003eall(); // Get all session variables\n    return $res-\u003esend('Session data: ' . json_encode($allSessionData));\n});\n```\n\n#### Checking if a Session Variable Exists\n\n```php\n$app-\u003eget('/has-user', function ($req, $res) {\n    $hasUser = $req-\u003esession-\u003ehas('user'); // Check if the session variable 'user' exists\n    return $res-\u003esend('User in session: ' . ($hasUser ? 'Yes' : 'No'));\n});\n```\n\n## 4. Magic Methods\n\nThe session object also provides some magic methods for convenience:\n\n- **`__get($key)`**: Retrieves the value of a session variable.\n  \n  ```php\n  $user = $req-\u003esession-\u003euser; // Equivalent to $req-\u003esession-\u003eget('user');\n  ```\n\n- **`__set($key, $value)`**: Sets the value of a session variable.\n  \n  ```php\n  $req-\u003esession-\u003euser = 'Jane Doe'; // Equivalent to $req-\u003esession-\u003eput('user', 'Jane Doe');\n  ```\n\n## Final Considerations\n\n- **Creating the Session Directory**: The middleware ensures that the directory for storing sessions exists. If it does not, it will be created automatically.\n- **Error Handling**: If any errors occur during session configuration or initialization, the middleware will log them and continue execution normally.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flithemod%2Fsession","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flithemod%2Fsession","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flithemod%2Fsession/lists"}