{"id":16329280,"url":"https://github.com/josantonius/php-session","last_synced_at":"2025-04-09T22:15:40.322Z","repository":{"id":37444858,"uuid":"79187205","full_name":"josantonius/php-session","owner":"josantonius","description":"PHP library for handling sessions","archived":false,"fork":false,"pushed_at":"2024-05-20T09:27:21.000Z","size":1237,"stargazers_count":75,"open_issues_count":1,"forks_count":28,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-09T22:15:35.696Z","etag":null,"topics":["cookie","php","php-sessions","session"],"latest_commit_sha":null,"homepage":"https://josantonius.dev","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/josantonius.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["Josantonius"]}},"created_at":"2017-01-17T04:12:28.000Z","updated_at":"2025-03-12T15:10:16.000Z","dependencies_parsed_at":"2022-07-12T13:34:43.136Z","dependency_job_id":null,"html_url":"https://github.com/josantonius/php-session","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-session","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-session/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-session/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-session/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/josantonius","download_url":"https://codeload.github.com/josantonius/php-session/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248119289,"owners_count":21050755,"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":["cookie","php","php-sessions","session"],"created_at":"2024-10-10T23:15:00.245Z","updated_at":"2025-04-09T22:15:40.295Z","avatar_url":"https://github.com/josantonius.png","language":"PHP","readme":"# PHP Session library\n\n[![Latest Stable Version](https://poser.pugx.org/josantonius/session/v/stable)](https://packagist.org/packages/josantonius/session)\n[![License](https://poser.pugx.org/josantonius/session/license)](LICENSE)\n[![Total Downloads](https://poser.pugx.org/josantonius/session/downloads)](https://packagist.org/packages/josantonius/session)\n[![CI](https://github.com/josantonius/php-session/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/josantonius/php-session/actions/workflows/ci.yml)\n[![CodeCov](https://codecov.io/gh/josantonius/php-session/branch/main/graph/badge.svg)](https://codecov.io/gh/josantonius/php-session)\n[![PSR1](https://img.shields.io/badge/PSR-1-f57046.svg)](https://www.php-fig.org/psr/psr-1/)\n[![PSR4](https://img.shields.io/badge/PSR-4-9b59b6.svg)](https://www.php-fig.org/psr/psr-4/)\n[![PSR12](https://img.shields.io/badge/PSR-12-1abc9c.svg)](https://www.php-fig.org/psr/psr-12/)\n\n**Translations**: [Español](.github/lang/es-ES/README.md)\n\nPHP library for handling sessions.\n\n---\n\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Available Classes](#available-classes)\n  - [Session Class](#session-class)\n  - [Session Facade](#session-facade)\n- [Exceptions Used](#exceptions-used)\n- [Usage](#usage)\n- [Tests](#tests)\n- [TODO](#todo)\n- [Changelog](#changelog)\n- [Contribution](#contribution)\n- [Sponsor](#sponsor)\n- [License](#license)\n\n---\n\n## Requirements\n\n- Operating System: Linux | Windows.\n\n- PHP versions: 8.0 | 8.1 | 8.2 | 8.3.\n\n## Installation\n\nThe preferred way to install this extension is through [Composer](http://getcomposer.org/download/).\n\nTo install **PHP Session library**, simply:\n\n```console\ncomposer require josantonius/session\n```\n\nThe previous command will only install the necessary files,\nif you prefer to **download the entire source code** you can use:\n\n```console\ncomposer require josantonius/session --prefer-source\n```\n\nYou can also **clone the complete repository** with Git:\n\n```console\ngit clone https://github.com/josantonius/php-session.git\n```\n\n## Available Classes\n\n### Session Class\n\n`Josantonius\\Session\\Session`\n\nStarts the session:\n\n```php\n/**\n * @throws HeadersSentException        if headers already sent.\n * @throws SessionStartedException     if session already started.\n * @throws WrongSessionOptionException if setting options failed.\n * \n * @see https://php.net/session.configuration for List of available $options.\n */\npublic function start(array $options = []): bool;\n```\n\nCheck if the session is started:\n\n```php\npublic function isStarted(): bool;\n```\n\nSets an attribute by name:\n\n```php\n/**\n * @throws SessionNotStartedException if session was not started.\n */\npublic function set(string $name, mixed $value): void;\n```\n\nGets an attribute by name:\n\n```php\n/**\n * Optionally defines a default value when the attribute does not exist.\n */\npublic function get(string $name, mixed $default = null): mixed;\n```\n\nGets all attributes:\n\n```php\npublic function all(): array;\n```\n\nCheck if an attribute exists in the session:\n\n```php\npublic function has(string $name): bool;\n```\n\nSets several attributes at once:\n\n```php\n/**\n * If attributes exist they are replaced, if they do not exist they are created.\n * \n * @throws SessionNotStartedException if session was not started.\n */\npublic function replace(array $data): void;\n```\n\nDeletes an attribute by name and returns its value:\n\n```php\n/**\n * Optionally defines a default value when the attribute does not exist.\n * \n * @throws SessionNotStartedException if session was not started.\n */\npublic function pull(string $name, mixed $default = null): mixed;\n```\n\nDeletes an attribute by name:\n\n```php\n/**\n * @throws SessionNotStartedException if session was not started.\n */\npublic function remove(string $name): void;\n```\n\nFree all session variables:\n\n```php\n/**\n * @throws SessionNotStartedException if session was not started.\n */\npublic function clear(): void;\n```\n\nGets the session ID:\n\n```php\npublic function getId(): string;\n```\n\nSets the session ID:\n\n```php\n/**\n * @throws SessionStartedException if session already started.\n */\npublic function setId(string $sessionId): void;\n```\n\nUpdate the current session ID with a newly generated one:\n\n```php\n/**\n * @throws SessionNotStartedException if session was not started.\n */\npublic function regenerateId(bool $deleteOldSession = false): bool;\n```\n\nGets the session name:\n\n```php\npublic function getName(): string;\n```\n\nSets the session name:\n\n```php\n/**\n * @throws SessionStartedException if session already started.\n */\npublic function setName(string $name): void;\n```\n\nDestroys the session:\n\n```php\n/**\n * @throws SessionNotStartedException if session was not started.\n */\npublic function destroy(): bool;\n```\n\n### Session Facade\n\n`Josantonius\\Session\\Facades\\Session`\n\nStarts the session:\n\n```php\n/**\n * @throws HeadersSentException        if headers already sent.\n * @throws SessionStartedException     if session already started.\n * @throws WrongSessionOptionException if setting options failed.\n * \n * @see https://php.net/session.configuration for List of available $options.\n */\npublic static function start(array $options = []): bool;\n```\n\nCheck if the session is started:\n\n```php\npublic static function isStarted(): bool;\n```\n\nSets an attribute by name:\n\n```php\n/**\n * @throws SessionNotStartedException if session was not started.\n */\npublic static function set(string $name, mixed $value): void;\n```\n\nGets an attribute by name:\n\n```php\n/**\n * Optionally defines a default value when the attribute does not exist.\n */\npublic static function get(string $name, mixed $default = null): mixed;\n```\n\nGets all attributes:\n\n```php\npublic static function all(): array;\n```\n\nCheck if an attribute exists in the session:\n\n```php\npublic static function has(string $name): bool;\n```\n\nSets several attributes at once:\n\n```php\n/**\n * If attributes exist they are replaced, if they do not exist they are created.\n * \n * @throws SessionNotStartedException if session was not started.\n */\npublic static function replace(array $data): void;\n```\n\nDeletes an attribute by name and returns its value:\n\n```php\n/**\n * Optionally defines a default value when the attribute does not exist.\n * \n * @throws SessionNotStartedException if session was not started.\n */\npublic static function pull(string $name, mixed $default = null): mixed;\n```\n\nDeletes an attribute by name:\n\n```php\n/**\n * @throws SessionNotStartedException if session was not started.\n */\npublic static function remove(string $name): void;\n```\n\nFree all session variables:\n\n```php\n/**\n * @throws SessionNotStartedException if session was not started.\n */\npublic static function clear(): void;\n```\n\nGets the session ID:\n\n```php\npublic static function getId(): string;\n```\n\nSets the session ID:\n\n```php\n/**\n * @throws SessionStartedException if session already started.\n */\npublic static function setId(string $sessionId): void;\n```\n\nUpdate the current session ID with a newly generated one:\n\n```php\n/**\n * @throws SessionNotStartedException if session was not started.\n */\npublic static function regenerateId(bool $deleteOldSession = false): bool;\n```\n\nGets the session name:\n\n```php\npublic static function getName(): string;\n```\n\nSets the session name:\n\n```php\n/**\n * @throws SessionStartedException if session already started.\n */\npublic static function setName(string $name): void;\n```\n\nDestroys the session:\n\n```php\n/**\n * @throws SessionNotStartedException if session was not started.\n */\npublic static function destroy(): bool;\n```\n\n## Exceptions Used\n\n```php\nuse Josantonius\\Session\\Exceptions\\HeadersSentException;\nuse Josantonius\\Session\\Exceptions\\SessionException;\nuse Josantonius\\Session\\Exceptions\\SessionNotStartedException;\nuse Josantonius\\Session\\Exceptions\\SessionStartedException;\nuse Josantonius\\Session\\Exceptions\\WrongSessionOptionException;\n```\n\n## Usage\n\nExample of use for this library:\n\n### Starts the session without setting options\n\n```php\nuse Josantonius\\Session\\Session;\n\n$session = new Session();\n\n$session-\u003estart();\n```\n\n```php\nuse Josantonius\\Session\\Facades\\Session;\n\nSession::start();\n```\n\n### Starts the session setting options\n\n```php\nuse Josantonius\\Session\\Session;\n\n$session = new Session();\n\n$session-\u003estart([\n    // 'cache_expire' =\u003e 180,\n    // 'cache_limiter' =\u003e 'nocache',\n    // 'cookie_domain' =\u003e '',\n    'cookie_httponly' =\u003e true,\n    'cookie_lifetime' =\u003e 8000,\n    // 'cookie_path' =\u003e '/',\n    'cookie_samesite' =\u003e 'Strict',\n    'cookie_secure'   =\u003e true,\n    // 'gc_divisor' =\u003e 100,\n    // 'gc_maxlifetime' =\u003e 1440,\n    // 'gc_probability' =\u003e true,\n    // 'lazy_write' =\u003e true,\n    // 'name' =\u003e 'PHPSESSID',\n    // 'read_and_close' =\u003e false,\n    // 'referer_check' =\u003e '',\n    // 'save_handler' =\u003e 'files',\n    // 'save_path' =\u003e '',\n    // 'serialize_handler' =\u003e 'php',\n    // 'sid_bits_per_character' =\u003e 4,\n    // 'sid_length' =\u003e 32,\n    // 'trans_sid_hosts' =\u003e $_SERVER['HTTP_HOST'],\n    // 'trans_sid_tags' =\u003e 'a=href,area=href,frame=src,form=',\n    // 'use_cookies' =\u003e true,\n    // 'use_only_cookies' =\u003e true,\n    // 'use_strict_mode' =\u003e false,\n    // 'use_trans_sid' =\u003e false,\n]);\n```\n\n```php\nuse Josantonius\\Session\\Facades\\Session;\n\nSession::start([\n    'cookie_httponly' =\u003e true,\n]);\n```\n\n### Check if the session is started\n\n```php\nuse Josantonius\\Session\\Session;\n\n$session = new Session();\n\n$session-\u003eisStarted();\n```\n\n```php\nuse Josantonius\\Session\\Facades\\Session;\n\nSession::isStarted();\n```\n\n### Sets an attribute by name\n\n```php\nuse Josantonius\\Session\\Session;\n\n$session = new Session();\n\n$session-\u003eset('foo', 'bar');\n```\n\n```php\nuse Josantonius\\Session\\Facades\\Session;\n\nSession::set('foo', 'bar');\n```\n\n### Gets an attribute by name without setting a default value\n\n```php\nuse Josantonius\\Session\\Session;\n\n$session = new Session();\n\n$session-\u003eget('foo'); // null if attribute does not exist\n```\n\n```php\nuse Josantonius\\Session\\Facades\\Session;\n\nSession::get('foo'); // null if attribute does not exist\n```\n\n### Gets an attribute by name setting a default value\n\n```php\nuse Josantonius\\Session\\Session;\n\n$session = new Session();\n\n$session-\u003eget('foo', false); // false if attribute does not exist\n```\n\n```php\nuse Josantonius\\Session\\Facades\\Session;\n\nSession::get('foo', false); // false if attribute does not exist\n```\n\n### Gets all attributes\n\n```php\nuse Josantonius\\Session\\Session;\n\n$session = new Session();\n\n$session-\u003eall();\n```\n\n```php\nuse Josantonius\\Session\\Facades\\Session;\n\nSession::all();\n```\n\n### Check if an attribute exists in the session\n\n```php\nuse Josantonius\\Session\\Session;\n\n$session = new Session();\n\n$session-\u003ehas('foo');\n```\n\n```php\nuse Josantonius\\Session\\Facades\\Session;\n\nSession::has('foo');\n```\n\n### Sets several attributes at once\n\n```php\nuse Josantonius\\Session\\Session;\n\n$session = new Session();\n\n$session-\u003ereplace(['foo' =\u003e 'bar', 'bar' =\u003e 'foo']);\n```\n\n```php\nuse Josantonius\\Session\\Facades\\Session;\n\nSession::replace(['foo' =\u003e 'bar', 'bar' =\u003e 'foo']);\n```\n\n### Deletes an attribute and returns its value or the default value if not exist\n\n```php\nuse Josantonius\\Session\\Session;\n\n$session = new Session();\n\n$session-\u003epull('foo'); // null if attribute does not exist\n```\n\n```php\nuse Josantonius\\Session\\Facades\\Session;\n\nSession::pull('foo'); // null if attribute does not exist\n```\n\n### Deletes an attribute and returns its value or the custom value if not exist\n\n```php\nuse Josantonius\\Session\\Session;\n\n$session = new Session();\n\n$session-\u003epull('foo', false); // false if attribute does not exist\n```\n\n```php\nuse Josantonius\\Session\\Facades\\Session;\n\nSession::pull('foo', false); // false if attribute does not exist\n```\n\n### Deletes an attribute by name\n\n```php\nuse Josantonius\\Session\\Session;\n\n$session = new Session();\n\n$session-\u003eremove('foo');\n```\n\n```php\nuse Josantonius\\Session\\Facades\\Session;\n\nSession::remove('foo');\n```\n\n### Free all session variables\n\n```php\nuse Josantonius\\Session\\Session;\n\n$session = new Session();\n\n$session-\u003eclear();\n```\n\n```php\nuse Josantonius\\Session\\Facades\\Session;\n\nSession::clear();\n```\n\n### Gets the session ID\n\n```php\nuse Josantonius\\Session\\Session;\n\n$session = new Session();\n\n$session-\u003egetId();\n```\n\n```php\nuse Josantonius\\Session\\Facades\\Session;\n\nSession::getId();\n```\n\n### Sets the session ID\n\n```php\nuse Josantonius\\Session\\Session;\n\n$session = new Session();\n\n$session-\u003esetId('foo');\n```\n\n```php\nuse Josantonius\\Session\\Facades\\Session;\n\nSession::setId('foo');\n```\n\n### Update the current session ID with a newly generated one\n\n```php\nuse Josantonius\\Session\\Session;\n\n$session = new Session();\n\n$session-\u003eregenerateId();\n```\n\n```php\nuse Josantonius\\Session\\Facades\\Session;\n\nSession::regenerateId();\n```\n\n### Update the current session ID with a newly generated one deleting the old session\n\n```php\nuse Josantonius\\Session\\Session;\n\n$session = new Session();\n\n$session-\u003eregenerateId(true);\n```\n\n```php\nuse Josantonius\\Session\\Facades\\Session;\n\nSession::regenerateId(true);\n```\n\n### Gets the session name\n\n```php\nuse Josantonius\\Session\\Session;\n\n$session = new Session();\n\n$session-\u003egetName();\n```\n\n```php\nuse Josantonius\\Session\\Facades\\Session;\n\nSession::getName();\n```\n\n### Sets the session name\n\n```php\nuse Josantonius\\Session\\Session;\n\n$session = new Session();\n\n$session-\u003esetName('foo');\n```\n\n```php\nuse Josantonius\\Session\\Facades\\Session;\n\nSession::setName('foo');\n```\n\n### Destroys the session\n\n```php\nuse Josantonius\\Session\\Session;\n\n$session = new Session();\n\n$session-\u003edestroy();\n```\n\n```php\nuse Josantonius\\Session\\Facades\\Session;\n\nSession::destroy();\n```\n\n## Tests\n\nTo run [tests](tests) you just need [composer](http://getcomposer.org/download/)\nand to execute the following:\n\n```console\ngit clone https://github.com/josantonius/php-session.git\n```\n\n```console\ncd php-session\n```\n\n```console\ncomposer install\n```\n\nRun unit tests with [PHPUnit](https://phpunit.de/):\n\n```console\ncomposer phpunit\n```\n\nRun code standard tests with [PHPCS](https://github.com/squizlabs/PHP_CodeSniffer):\n\n```console\ncomposer phpcs\n```\n\nRun [PHP Mess Detector](https://phpmd.org/) tests to detect inconsistencies in code style:\n\n```console\ncomposer phpmd\n```\n\nRun all previous tests:\n\n```console\ncomposer tests\n```\n\n## TODO\n\n- [ ] Add new feature\n- [ ] Improve tests\n- [ ] Improve documentation\n- [ ] Improve English translation in the README file\n- [ ] Refactor code for disabled code style rules (see phpmd.xml and phpcs.xml)\n- [ ] Show an example of renewing the session lifetime\n- [ ] Feature to enable/disable exceptions?\n- [ ] Feature to add prefixes in session attributes?\n\n## Changelog\n\nDetailed changes for each release are documented in the\n[release notes](https://github.com/josantonius/php-session/releases).\n\n## Contribution\n\nPlease make sure to read the [Contributing Guide](.github/CONTRIBUTING.md), before making a pull\nrequest, start a discussion or report a issue.\n\nThanks to all [contributors](https://github.com/josantonius/php-session/graphs/contributors)! :heart:\n\n## Sponsor\n\nIf this project helps you to reduce your development time,\n[you can sponsor me](https://github.com/josantonius#sponsor) to support my open source work :blush:\n\n## License\n\nThis repository is licensed under the [MIT License](LICENSE).\n\nCopyright © 2017-present, [Josantonius](https://github.com/josantonius#contact)\n","funding_links":["https://github.com/sponsors/Josantonius"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosantonius%2Fphp-session","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjosantonius%2Fphp-session","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosantonius%2Fphp-session/lists"}