{"id":16329261,"url":"https://github.com/josantonius/php-cookie","last_synced_at":"2025-03-15T22:10:02.133Z","repository":{"id":41258141,"uuid":"76510593","full_name":"josantonius/php-cookie","owner":"josantonius","description":"PHP library for handling cookies","archived":false,"fork":false,"pushed_at":"2024-09-11T14:21:41.000Z","size":1336,"stargazers_count":21,"open_issues_count":0,"forks_count":12,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-28T20:36:39.996Z","etag":null,"topics":["cookie","cookies","handling-cookies","php","php-cookie","php-library"],"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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["Josantonius"]}},"created_at":"2016-12-15T01:04:59.000Z","updated_at":"2025-02-07T14:45:51.000Z","dependencies_parsed_at":"2025-02-07T13:11:02.772Z","dependency_job_id":"41d59dbd-25e7-418b-a240-7677a50dfe1b","html_url":"https://github.com/josantonius/php-cookie","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-cookie","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-cookie/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-cookie/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-cookie/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/josantonius","download_url":"https://codeload.github.com/josantonius/php-cookie/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243796728,"owners_count":20349263,"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","cookies","handling-cookies","php","php-cookie","php-library"],"created_at":"2024-10-10T23:14:56.851Z","updated_at":"2025-03-15T22:10:02.113Z","avatar_url":"https://github.com/josantonius.png","language":"PHP","funding_links":["https://github.com/sponsors/Josantonius"],"categories":[],"sub_categories":[],"readme":"# PHP Cookie library\n\n[![Latest Stable Version](https://poser.pugx.org/josantonius/cookie/v/stable)](https://packagist.org/packages/josantonius/cookie)\n[![License](https://poser.pugx.org/josantonius/cookie/license)](LICENSE)\n[![Total Downloads](https://poser.pugx.org/josantonius/cookie/downloads)](https://packagist.org/packages/josantonius/cookie)\n[![CI](https://github.com/josantonius/php-cookie/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/josantonius/php-cookie/actions/workflows/ci.yml)\n[![CodeCov](https://codecov.io/gh/josantonius/php-cookie/branch/main/graph/badge.svg)](https://codecov.io/gh/josantonius/php-cookie)\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 cookies.\n\n---\n\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Available Classes](#available-classes)\n  - [Cookie Class](#cookie-class)\n  - [Cookie Facade](#cookie-facade)\n- [Exceptions Used](#exceptions-used)\n- [Usage](#usage)\n- [About Cookie Expires](#about-cookie-expires)\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.\n\n- PHP versions: 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 Cookie library**, simply:\n\n```console\ncomposer require josantonius/cookie\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/cookie --prefer-source\n```\n\nYou can also **clone the complete repository** with Git:\n\n```console\ngit clone https://github.com/josantonius/php-cookie.git\n```\n\n## Available Classes\n\n### Cookie Class\n\n`Josantonius\\Cookie\\Cookie`\n\nSets cookie options:\n\n```php\n/**\n * Cookie options:\n * \n * domain:   Domain for which the cookie is available.\n * expires:  The time the cookie will expire.\n * httpOnly: If cookie will only be available through the HTTP protocol.\n * path:     Path for which the cookie is available.\n * raw:      If cookie will be sent as a raw string.\n * sameSite: Enforces the use of a Lax or Strict SameSite policy.\n * secure:   If cookie will only be available through the HTTPS protocol.\n * \n * These settings will be used to create and delete cookies.\n * \n * @throws CookieException if $sameSite value is wrong.\n *\n * @see https://www.php.net/manual/en/datetime.formats.php for date formats.\n * @see https://www.php.net/manual/en/function.setcookie.php for more information.\n */\npublic function __construct(\n    private string              $domain   = '',\n    private int|string|DateTime $expires  = 0,\n    private bool                $httpOnly = false,\n    private string              $path     = '/',\n    private bool                $raw      = false,\n    private null|string         $sameSite = null,\n    private bool                $secure   = false\n);\n```\n\nSets a cookie by name:\n\n```php\n/**\n * @throws CookieException if headers already sent.\n * @throws CookieException if failure in date/time string analysis.\n */\npublic function set(\n    string $name,\n    mixed $value,\n    null|int|string|DateTime $expires = null\n): void;\n```\n\nSets several cookies at once:\n\n```php\n/**\n * If cookies exist they are replaced, if they do not exist they are created.\n *\n * @throws CookieException if headers already sent.\n */\npublic function replace(\n    array $data,\n    null|int|string|DateTime $expires = null\n): void;\n```\n\nGets a cookie by name:\n\n```php\n/**\n * Optionally defines a default value when the cookie does not exist.\n */\npublic function get(string $name, mixed $default = null): mixed;\n```\n\nGets all cookies:\n\n```php\npublic function all(): array;\n```\n\nCheck if a cookie exists:\n\n```php\npublic function has(string $name): bool;\n```\n\nDeletes a cookie by name and returns its value:\n\n```php\n/**\n * Optionally defines a default value when the cookie does not exist.\n * \n * @throws CookieException if headers already sent.\n */\npublic function pull(string $name, mixed $default = null): mixed;\n```\n\nDeletes an cookie by name:\n\n```php\n/**\n * @throws CookieException if headers already sent.\n * @throws CookieException if failure in date/time string analysis.\n */\npublic function remove(string $name): void;\n```\n\n### Cookie Facade\n\n`Josantonius\\Cookie\\Facades\\Cookie`\n\nSets cookie options:\n\n```php\n/**\n * Cookie options:\n * \n * domain:   Domain for which the cookie is available.\n * expires:  The time the cookie will expire.\n * httpOnly: If cookie will only be available through the HTTP protocol.\n * path:     Path for which the cookie is available.\n * raw:      If cookie will be sent as a raw string.\n * sameSite: Enforces the use of a Lax or Strict SameSite policy.\n * secure:   If cookie will only be available through the HTTPS protocol.\n * \n * These settings will be used to create and delete cookies.\n * \n * @throws CookieException if $sameSite value is wrong.\n *\n * @see https://www.php.net/manual/en/datetime.formats.php for date formats.\n * @see https://www.php.net/manual/en/function.setcookie.php for more information.\n */\npublic static function options(\n    string              $domain   = '',\n    int|string|DateTime $expires  = 0,\n    bool                $httpOnly = false,\n    string              $path     = '/',\n    bool                $raw      = false,\n    null|string         $sameSite = null,\n    bool                $secure   = false\n): void;\n```\n\nSets a cookie by name:\n\n```php\n/**\n * @throws CookieException if headers already sent.\n * @throws CookieException if failure in date/time string analysis.\n */\npublic static function set(\n    string $name,\n    mixed $value,\n    null|int|string|DateTime $expires = null\n): void;\n```\n\nSets several cookies at once:\n\n```php\n/**\n * If cookies exist they are replaced, if they do not exist they are created.\n *\n * @throws CookieException if headers already sent.\n */\npublic static function replace(\n    array $data,\n    null|int|string|DateTime $expires = null\n): void;\n```\n\nGets a cookie by name:\n\n```php\n/**\n * Optionally defines a default value when the cookie does not exist.\n */\npublic static function get(string $name, mixed $default = null): mixed;\n```\n\nGets all cookies:\n\n```php\npublic static function all(): array;\n```\n\nCheck if a cookie exists:\n\n```php\npublic static function has(string $name): bool;\n```\n\nDeletes a cookie by name and returns its value:\n\n```php\n/**\n * Optionally defines a default value when the cookie does not exist.\n * \n * @throws CookieException if headers already sent.\n */\npublic static function pull(string $name, mixed $default = null): mixed;\n```\n\nDeletes an cookie by name:\n\n```php\n/**\n * @throws CookieException if headers already sent.\n * @throws CookieException if failure in date/time string analysis.\n */\npublic static function remove(string $name): void;\n```\n\n## Exceptions Used\n\n```php\nuse Josantonius\\Cookie\\Exceptions\\CookieException;\n```\n\n## Usage\n\nExample of use for this library:\n\n### Create Cookie instance with default options\n\n```php\nuse Josantonius\\Cookie\\Cookie;\n\n$cookie = new Cookie();\n```\n\n```php\nuse Josantonius\\Cookie\\Facades\\Cookie;\n\nCookie::options();\n```\n\n### Create Cookie instance with custom options\n\n```php\nuse Josantonius\\Cookie\\Cookie;\n\n$cookie = new Cookie(\n    domain: 'example.com',\n    expires: time() + 3600,\n    httpOnly: true,\n    path: '/foo',\n    raw: true,\n    sameSite: 'Strict',\n    secure: true,\n);\n```\n\n```php\nuse Josantonius\\Cookie\\Facades\\Cookie;\n\nCookie::options(\n    expires: 'now +1 hour',\n    httpOnly: true,\n);\n```\n\n### Sets a cookie by name\n\n```php\nuse Josantonius\\Cookie\\Cookie;\n\n$cookie = new Cookie();\n\n$cookie-\u003eset('foo', 'bar');\n```\n\n```php\nuse Josantonius\\Cookie\\Facades\\Cookie;\n\nCookie::set('foo', 'bar');\n```\n\n### Sets a cookie by name modifying the expiration time\n\n```php\nuse Josantonius\\Cookie\\Cookie;\n\n$cookie = new Cookie();\n\n$cookie-\u003eset('foo', 'bar', time() + 3600);\n```\n\n```php\nuse Josantonius\\Cookie\\Facades\\Cookie;\n\nCookie::set('foo', 'bar', new DateTime('now +1 hour'));\n```\n\n### Sets several cookies at once\n\n```php\nuse Josantonius\\Cookie\\Cookie;\n\n$cookie = new Cookie();\n\n$cookie-\u003ereplace([\n    'foo' =\u003e 'bar',\n    'bar' =\u003e 'foo'\n]);\n```\n\n```php\nuse Josantonius\\Cookie\\Facades\\Cookie;\n\nCookie::replace([\n    'foo' =\u003e 'bar',\n    'bar' =\u003e 'foo'\n], time() + 3600);\n```\n\n### Sets several cookies at once modifying the expiration time\n\n```php\nuse Josantonius\\Cookie\\Cookie;\n\n$cookie = new Cookie();\n\n$cookie-\u003ereplace([\n    'foo' =\u003e 'bar',\n    'bar' =\u003e 'foo'\n], time() + 3600);\n```\n\n```php\nuse Josantonius\\Cookie\\Facades\\Cookie;\n\nCookie::replace([\n    'foo' =\u003e 'bar',\n    'bar' =\u003e 'foo'\n], time() + 3600);\n```\n\n### Gets a cookie by name\n\n```php\nuse Josantonius\\Cookie\\Cookie;\n\n$cookie = new Cookie();\n\n$cookie-\u003eget('foo'); // null if the cookie does not exist\n```\n\n```php\nuse Josantonius\\Cookie\\Facades\\Cookie;\n\nCookie::get('foo'); // null if the cookie does not exist\n```\n\n### Gets a cookie by name with default value if cookie does not exist\n\n```php\nuse Josantonius\\Cookie\\Cookie;\n\n$cookie = new Cookie();\n\n$cookie-\u003eget('foo', false); // false if cookie does not exist\n```\n\n```php\nuse Josantonius\\Cookie\\Facades\\Cookie;\n\nCookie::get('foo', false); // false if cookie does not exist\n```\n\n### Gets all cookies\n\n```php\nuse Josantonius\\Cookie\\Cookie;\n\n$cookie = new Cookie();\n\n$cookie-\u003eall();\n```\n\n```php\nuse Josantonius\\Cookie\\Facades\\Cookie;\n\nCookie::all();\n```\n\n### Check if a cookie exists\n\n```php\nuse Josantonius\\Cookie\\Cookie;\n\n$cookie = new Cookie();\n\n$cookie-\u003ehas('foo');\n```\n\n```php\nuse Josantonius\\Cookie\\Facades\\Cookie;\n\nCookie::has('foo');\n```\n\n### Deletes a cookie by name and returns its value\n\n```php\nuse Josantonius\\Cookie\\Cookie;\n\n$cookie = new Cookie();\n\n$cookie-\u003epull('foo'); // null if attribute does not exist\n```\n\n```php\nuse Josantonius\\Cookie\\Facades\\Cookie;\n\nCookie::pull('foo'); // null if attribute does not exist\n```\n\n### Deletes a cookie and returns its value or default value if does not exist\n\n```php\nuse Josantonius\\Cookie\\Cookie;\n\n$cookie = new Cookie();\n\n$cookie-\u003epull('foo', false); // false if attribute does not exist\n```\n\n```php\nuse Josantonius\\Cookie\\Facades\\Cookie;\n\nCookie::pull('foo', false); // false if attribute does not exist\n```\n\n### Deletes an cookie by name\n\n```php\nuse Josantonius\\Cookie\\Cookie;\n\n$cookie = new Cookie();\n\n$cookie-\u003eremove('foo');\n```\n\n```php\nuse Josantonius\\Cookie\\Facades\\Cookie;\n\nCookie::remove('foo');\n```\n\n## About cookie expires\n\n- The **expires** parameter used in several methods of this library accepts the following types:\n`int|string|DateTime`.\n\n  - `Integers` will be handled as unix time except zero.\n  - `Strings` will be handled as date/time formats.\n  See supported [Date and Time Formats](https://www.php.net/manual/en/datetime.formats.php)\n  for more information.\n\n    ```php\n    $cookie = new Cookie(\n        expires: '2016-12-15 +1 day'\n    );\n    ```\n\n    It would be similar to:\n\n    ```php\n    $cookie = new Cookie(\n        expires: new DateTime('2016-12-15 +1 day')\n    );\n    ```\n\n  - `DateTime` objects will be used to obtain the unix time.\n\n- If the **expires** parameter is used in the `set` or `replace` methods,\nit will be taken instead of the **expires** value set in the cookie options.\n\n    ```php\n    $cookie = new Cookie(\n        expires: 'now +1 minute'\n    );\n\n    $cookie-\u003eset('foo', 'bar');                        // Expires in 1 minute\n\n    $cookie-\u003eset('bar', 'foo', 'now +8 days');         // Expires in 8 days\n\n    $cookie-\u003ereplace(['foo' =\u003e 'bar']);                // Expires in 1 minute\n\n    $cookie-\u003ereplace(['foo' =\u003e 'bar'], time() + 3600); // Expires in 1 hour\n    ```\n\n- If the **expires** parameter passed in the options is a date/time string,\nit is formatted when using the `set` or `replace` method and not when setting the options.\n\n    ```php\n    $cookie = new Cookie(\n        expires: 'now +1 minute', // It will not be formatted as unix time yet\n    );\n\n    $cookie-\u003eset('foo', 'bar');   // It is will formatted now and expires in 1 minute\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-cookie.git\n```\n\n```console\ncd php-cookie\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\n## Changelog\n\nDetailed changes for each release are documented in the\n[release notes](https://github.com/josantonius/php-cookie/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-cookie/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 © 2016-present, [Josantonius](https://github.com/josantonius#contact)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosantonius%2Fphp-cookie","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjosantonius%2Fphp-cookie","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosantonius%2Fphp-cookie/lists"}