{"id":19665907,"url":"https://github.com/amphp/http-server-session","last_synced_at":"2025-04-28T22:31:29.698Z","repository":{"id":56947310,"uuid":"36581411","full_name":"amphp/http-server-session","owner":"amphp","description":"An HTTP server plugin that simplifies session management for your applications. Effortlessly handle user sessions, securely managing data across requests.","archived":false,"fork":false,"pushed_at":"2024-02-15T19:35:09.000Z","size":215,"stargazers_count":19,"open_issues_count":1,"forks_count":8,"subscribers_count":9,"default_branch":"3.x","last_synced_at":"2025-04-05T11:34:16.571Z","etag":null,"topics":["amphp","php","revolt","session"],"latest_commit_sha":null,"homepage":"https://amphp.org/http-server-session","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/amphp.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}},"created_at":"2015-05-31T00:32:16.000Z","updated_at":"2025-03-05T20:16:45.000Z","dependencies_parsed_at":"2024-06-21T16:35:27.587Z","dependency_job_id":"c2f42254-6906-43b2-9fdf-4bfd1e979cda","html_url":"https://github.com/amphp/http-server-session","commit_stats":{"total_commits":173,"total_committers":7,"mean_commits":"24.714285714285715","dds":"0.49710982658959535","last_synced_commit":"dac3188c9cb368e635cb8a4e4ff84bf128bb6818"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amphp%2Fhttp-server-session","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amphp%2Fhttp-server-session/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amphp%2Fhttp-server-session/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amphp%2Fhttp-server-session/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amphp","download_url":"https://codeload.github.com/amphp/http-server-session/tar.gz/refs/heads/3.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251069082,"owners_count":21531566,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["amphp","php","revolt","session"],"created_at":"2024-11-11T16:25:24.296Z","updated_at":"2025-04-28T22:31:27.273Z","avatar_url":"https://github.com/amphp.png","language":"PHP","funding_links":[],"categories":["HTTP"],"sub_categories":["Server"],"readme":"# http-server-session\n\nAMPHP is a collection of event-driven libraries for PHP designed with fibers and concurrency in mind. This package provides an [HTTP server](https://amphp.org/http-server) plugin that simplifies session management for your applications. Effortlessly handle user sessions, securely managing data across requests.\n\n## Installation\n\nThis package can be installed as a [Composer](https://getcomposer.org/) dependency.\n\n```bash\ncomposer require amphp/http-server-session\n```\n\n## Usage\n\n#### Basic usage\n\nTo read data from the session is straightforward:\n\n```php\n$session-\u003eget('key'); // will read data stored in key 'key'\n```\n\nNote that `get()` will return `null` if the data in `key` are not found.\n\nIn order to write data, the session must be `lock()`ed first so that it cannot be written from anywhere else.\n\n```php\n$session-\u003elock();\n$session-\u003eset('key', $data);\n$session-\u003ecommit(); // commits \u0026 unlocks\n```\n\nCalling `commit()` will store the data in the session storage and unlock the session.\n\nOther important methods of the `Session` class are:\n\n```php\n// regenerate the client id\n$session-\u003eregenerate();\n\n// force read from storage\n$session-\u003eread();\n\n// rollback what is `set()` in the session but has not been commit()ed yet\n$session-\u003erollback();\n\n// destroy the session\n$session-\u003edestroy();\n```\n\n\n#### Use the middleware to access Session in a RequestHandler\n\nAs this package is a plugin for [`amphp/http-server`](https://github.com/amphp/http-server) there is a middleware\nimplementation available that injects the `Session` instance into the attributes of the `Request`. When the middleware\nis used the session is accessible from the attributes:\n\n```php\nuse Amp\\Http\\Server\\Request;\nuse Amp\\Http\\Server\\RequestHandler;\nuse Amp\\Http\\Server\\Response;\nuse Amp\\Http\\Server\\Session\\Session;\n\nclass SomeRequestHandler implements RequestHandler\n{\n    public function handleRequest(Request $request): Response\n    {\n        /** @var Session $session */\n        $session = $request-\u003egetAttribute(Session::class);\n\n        // any operations on the session\n\n        // return the response\n    }\n}\n```\n\nNote that if the attribute `Session::class` is not registered then `getAttribute` will throw a `MissingAttributeError`.\n\nThe middleware will handle setting and reading a session cookie in the request/response as well as releasing all locks\non the session after the request has been processed.\n\nIf you haven't used middleware in `amphp/http-server`, follow the [instructions on how to use middle ware with `amphp/http-server`](https://github.com/amphp/http-server#middleware).\n\nA simple example is provided here [`examples/simple.php`](https://github.com/amphp/http-server-session/blob/3.x/examples/simple.php).\n\nThe `SessionMiddleware` can be further configured from the constructor regarding four different aspects:\n\n* `SessionFactory`\n* `CookieAttributes`\n* Cookie name (default: `'session'`)\n* Request attribute (default: `Session::class`)\n\nThe `CookieAttributes` is used to configure different cookie properties such as the expiry or the domain:\n\n```php\n$cookieAttributes = CookieAttributes::default()\n    -\u003ewithDomain('amphp.org')\n    -\u003ewithExpiry(new \\DateTime('+30 min'))\n    -\u003ewithSecure();\n```\n\n#### Using the factory to create an instance of Session\n\nInternally the session works with 3 dependencies:\n\n* [`KeyedMutex`](https://github.com/amphp/sync/blob/2.x/src/KeyedMutex.php) - A synchronisation primitive to be used across contexts\n* [`SessionStorage`](https://github.com/amphp/http-server-session/blob/3.x/src/SessionStorage.php) - Interface for reading and writing data.\n* [`SessionIdGenerator`](https://github.com/amphp/http-server-session/blob/3.x/src/SessionIdGenerator.php) - Interface for generating and validating\n  a session ID.\n\nAn instance of the [`Session`](https://github.com/amphp/http-server-session/blob/3.x/src/Session.php#L28) can be constructed easily using\nthe provided [`SessionFactory`](https://github.com/amphp/http-server-session/blob/3.x/src/SessionFactory.php)\n\n```php\n/** @var \\Amp\\Http\\Server\\Session\\SessionFactory $factory */\n$session = $factory-\u003ecreate($clientId);\n```\n\nThis library comes with two storage implementations\n\n* LocalSessionStorage - Default\n* RedisSessionStorage - Storage in Redis\n\nand one session ID generator\n\n* Base64UrlSessionIdGenerator\n\nThe constructor of the `SessionFactory` allows to configure the factory with other implementations, so that subsequent\ncalls to `create()` will use the new injected implementations. This can be beneficial in the certain scenarios, including\ntesting.\n\n## Contributing\n\nPlease read [our rules](https://amphp.org/contributing) for details on our code of conduct, and the process for submitting pull requests to us.\n\n## Security\n\nIf you discover any security related issues, please use the private security issue reporter instead of using the public issue tracker.\n\n## License\n\nThe MIT License (MIT). Please see [LICENSE](./LICENSE) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famphp%2Fhttp-server-session","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famphp%2Fhttp-server-session","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famphp%2Fhttp-server-session/lists"}