{"id":18003372,"url":"https://github.com/ojhaujjwal/psr7-session","last_synced_at":"2025-07-12T11:03:09.674Z","repository":{"id":57074895,"uuid":"97329800","full_name":"ojhaujjwal/psr7-session","owner":"ojhaujjwal","description":"Alternative to PHP's native session handler for PSR-7.","archived":false,"fork":false,"pushed_at":"2020-08-07T20:32:20.000Z","size":41,"stargazers_count":7,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-07T05:34:29.256Z","etag":null,"topics":["http-middleware","http-session","php-middleware","php-sessions","psr-7","session-cookie","session-handler","session-management","session-middleware"],"latest_commit_sha":null,"homepage":null,"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/ojhaujjwal.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}},"created_at":"2017-07-15T16:54:11.000Z","updated_at":"2024-03-01T03:33:29.000Z","dependencies_parsed_at":"2022-08-24T14:55:38.356Z","dependency_job_id":null,"html_url":"https://github.com/ojhaujjwal/psr7-session","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/ojhaujjwal/psr7-session","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ojhaujjwal%2Fpsr7-session","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ojhaujjwal%2Fpsr7-session/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ojhaujjwal%2Fpsr7-session/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ojhaujjwal%2Fpsr7-session/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ojhaujjwal","download_url":"https://codeload.github.com/ojhaujjwal/psr7-session/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ojhaujjwal%2Fpsr7-session/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264979757,"owners_count":23692491,"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":["http-middleware","http-session","php-middleware","php-sessions","psr-7","session-cookie","session-handler","session-management","session-middleware"],"created_at":"2024-10-29T23:26:37.647Z","updated_at":"2025-07-12T11:03:09.398Z","avatar_url":"https://github.com/ojhaujjwal.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"PSR-7 Session\n===================\n[![Build Status][travis-image]][travis-url]\n[![Coverage Status][coverage-image]][coverage-url]\n[![Latest Stable Version](https://poser.pugx.org/ujjwal/psr7-http-session/v/stable)](https://packagist.org/packages/ujjwal/psr7-http-session)\n[![Total Downloads](https://poser.pugx.org/ujjwal/psr7-http-session/downloads)](https://packagist.org/packages/ujjwal/psr7-http-session)\n[![Latest Unstable Version](https://poser.pugx.org/ujjwal/psr7-http-session/v/unstable)](https://packagist.org/packages/ujjwal/psr7-http-session)\n[![License](https://poser.pugx.org/ujjwal/psr7-http-session/license)](https://packagist.org/packages/ujjwal/psr7-http-session)\n\nAlternative to PHP's native session handler. It does not depend on PHP's session capability. It can be used with non-typical php based applications like with [react/http](https://github.com/reactphp/http).\n\n### But, why?\n![But, why?](http://vignette2.wikia.nocookie.net/vampirediaries/images/c/ca/But-why-meme-generator-but-why-84103d.jpg/revision/latest?cb=20130811194815])\n- You don't have to depend on `session_` functions which means you can write testable code.\n- You don't have to depend on `$_SESSION` superglobal allowing you to write more testable code. \n- You can even use this for non-typical php based applications like with [react/http](https://github.com/reactphp/http).\n- You can create a framework agnostic library/module depending on [psr-7](http://www.php-fig.org/psr/psr-7/) HTTP message interfaces and this session library.\n\n## Getting started\n\n```php\n$sessionOptions = [\n    'name' =\u003e 'session_id',\n    'sid_length' =\u003e 40,\n    'cookie' =\u003e [\n        'domain' =\u003e 'your-app.com',\n    ]\n];\n\n$sessionHandler = new Ojhaujjwal\\Session\\Handler\\FileHandler('path/to/session-data');\n$sessionManager = new Ojhaujjwal\\Session\\SessionManager(\n    $sessionHandler,\n    $request,\n    $sessionOptions\n);\n$storage = $sessionManager-\u003egetStorage();\n\n$sessionManager-\u003estart();\n\n// you can manipulate $storage just like $_SESSION   \n$storage['some_key'] = 'some_value';\n$someKey = $storage['some_key'];\n\n$response = $sessionManager-\u003eclose($response);\n//return the response the the client\n```\n\n### Installation\n`composer require ujjwal/psr7-http-session`\n\n### Session Options\n\n##### name\nType: string\nRequired: true\n\nName of the session which is used as cookie name. It should only contain alphanumeric characters.\n\n\n#### sid_length\nType: integer\nDefault: 40\n\nthe length of session ID string. Session ID length can be between 22 to 256.\n\n#### cookie\nType: array\n\nUsed to pass cookie options. See cookie options section.\n\n### Cookie Options\n\n#### domain\nType: string\nDefault: derived from the `Host` header of request\n\ndomain to be set in the session cookie. \n\n#### path\nType: string\nDefault: `/`\n\npath to be set in the session cookie.\n\n#### http_only\nType: boolean\nDefault: `true`\n\nMarks the cookie as accessible only through the HTTP protocol. This means that the cookie won't be accessible by scripting languages, such as JavaScript.\n\n#### secure_only\nType: boolean\nDefault: True if the original request is https\n\nIt indicates whether cookies should only be sent over secure connections.\n\n#### lifetime\nType: integer\nDefault: `0` for session cookie\n\nIt specifies the lifetime of the cookie in seconds which is sent to the browser. The value 0 means \"until the browser is closed.\" Defaults to 0\n\n#### same_site\nType: string\nDefault: `Lax`\nSpecifies `SameSite` cookie attribute. Very useful to mitigate CSRF by preventing the browser from sending this cookie along with cross-site requests.\nAllowed values:\n* empty string for not setting the attribute\n* `ParagonIE\\Cookie\\Cookie::SAME_SITE_RESTRICTION_LAX`(fairly strict)\n* `ParagonIE\\Cookie\\Cookie::SAME_SITE_RESTRICTION_STRICT`(very strict) \n\n### Basic operations\n#### Initializing SessionManager\n```php\n$sessionManager = new Ojhaujjwal\\Session\\SessionManager(\n    $sessionHandler,\n    $request,\n    $sessionOptions\n);\n```\n\n#### Starting session\n```php\n$sessionManager-\u003estart();\n\n$sessionManager-\u003eisStarted(); // returns true\n```\n\n#### Retrieve session id\n```php\n$sessionManager-\u003egetId(); //returns alphanumeric string\n```\n\n#### Regenerate session id\n```php\n$sessionManager-\u003eregenerate();\n\n$sessionManager-\u003eregenerate(false); // does not destroy old session\n```\n\n#### Close session and write to response header as cookie \n```php\n$response = $sessionManager-\u003eclose($response);\n```\n\n#### Retrieving session storage \n```php\n$storage = $sessionManager-\u003egetStorage();\n```\nIt implements `IteratorAggregate`, `ArrayAccess`, `Countable`\nSo, it will look very much like `$_SESSION`. \nJust replace the `$_SESSION` occurrences in your app with instance of the object.\n\n#### Write to session \n```php\n$storage-\u003eabcd = 'efgh';\n//or\n$storage['abcd'] = 'efgh';\n//or\n$storage-\u003eset('abcd', 'efgh');\n```\n\n#### Read from session \n```php\n$abcd =  $storage-\u003eabc;\n//or\n$abcd = $storage['abcd'];\n//or\n$abcd = $storage-\u003eget('abcd');\n```\n\n#### Remove from session \n```php\nunset($storage-\u003eabc);\n//or\nunset($storage['abcd']);\n//or\n$storage-\u003eremove('abcd');\n```\n\n#### Flush session data\n```php\n$storage-\u003eflush();\n```\n\n### Session Middleware\nIt also comes with a http middleware which you can use to automatically initialize session and write cookie to response.\nThe middleware is compatible with `http-interop/http-middleware` based single pass approach or express-like double pass approach.  \n\n```php\n $middleware = new Ojhaujjwal\\Session\\SessionMiddleware($handler, $sessionOptions);\n $middleware-\u003eprocess($request, $delegate);\n // or\n $middleware($request, $response, $next);\n \n //using with zend-expressive\n //after errorhandler and before the routing middleware\n $app-\u003epipe(\\Ojhaujjwal\\Session\\SessionMiddleware::class);\n```\n \n### TODO \n- [ ] Fix build in php7.2\n- [ ] Garbage collection\n- [ ] Cookie Based session handler\n- [ ] Encryption Session Handler\n \n \n## License\n\n[MIT](LICENSE)\n\n[travis-image]: https://travis-ci.org/ojhaujjwal/psr7-session.svg?branch=master\n[travis-url]: https://travis-ci.org/ojhaujjwal/psr7-session\n[coverage-image]: https://coveralls.io/repos/github/ojhaujjwal/psr7-session/badge.svg?branch=master\n[coverage-url]: https://coveralls.io/github/ojhaujjwal/psr7-session?branch=master\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fojhaujjwal%2Fpsr7-session","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fojhaujjwal%2Fpsr7-session","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fojhaujjwal%2Fpsr7-session/lists"}