{"id":18463718,"url":"https://github.com/commandstring/reactphp-cookies","last_synced_at":"2025-10-31T09:30:37.976Z","repository":{"id":65378606,"uuid":"591217005","full_name":"CommandString/ReactPHP-Cookies","owner":"CommandString","description":"An easier way to manipulate cookies in React/Http","archived":false,"fork":false,"pushed_at":"2023-01-20T21:45:04.000Z","size":15,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-25T07:50:01.866Z","etag":null,"topics":[],"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/CommandString.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":"2023-01-20T07:51:19.000Z","updated_at":"2023-02-09T01:53:08.000Z","dependencies_parsed_at":"2023-02-12T05:30:30.643Z","dependency_job_id":null,"html_url":"https://github.com/CommandString/ReactPHP-Cookies","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CommandString%2FReactPHP-Cookies","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CommandString%2FReactPHP-Cookies/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CommandString%2FReactPHP-Cookies/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CommandString%2FReactPHP-Cookies/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CommandString","download_url":"https://codeload.github.com/CommandString/ReactPHP-Cookies/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239162320,"owners_count":19592340,"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":[],"created_at":"2024-11-06T09:07:47.079Z","updated_at":"2025-10-31T09:30:32.722Z","avatar_url":"https://github.com/CommandString.png","language":"PHP","readme":"\n# [CommandString/ReactPHP-cookies](https://packagist.org/packages/commandstring/reactphp-cookies) - A easier way to manipulate cookies in React/Http #\n\n### Install with Composer using `composer require commandstring/reactphp-cookies` ###\n\n*For the examples $req is an object that implements of PSR-7 ServerRequestInterface and $res is an object that implements PSR-7 ResponseInterface*\n\n# Creating Controller\n\n```php\n$cookieController = new CookieController(null);\n```\n\nIf you want to encrypt your cookies you can either create a class that implements CookieEncryptionInterface or use [Cookie Encryption](https://github.com/CommandString/Cookie-Encryption)\n\nCreating Cookie object from controller\n\nYou will need to create an object that implements PSR-7's Response Interface beforehand\n\n```php\n$cookie = $cookieController-\u003ecookie($req, $res);\n```\n\n# Setting cookies\n\n```php\n$cookie-\u003eset(\"token\", \"123456\", 1, 15, 13, \"/app\", \"app.domain.com\");\n```\n\nThis will create a cookie with the name of `token` that is set to `123456`. This cookie will expire in 1 hour, 15 minutes, and 13 seconds from now. The cookie is valid only in the app path and on the app.domain.com website.\n\n# Getting cookies\n\n```php\n$cookie-\u003eget(\"token\");\n```\n\nIf a cookie with the name of token is set then it will return it's value. If not it returns null.\n\n# Deleting cookies\n\n```php\n$cookie-\u003edelete(\"token\", \"/app\", \"app.domain.com\");\n```\n\nThis will delete a cookie with the name token that has its path set to `/app` and domain set to  `app.domain.com`\n\n# Example usage\n\n```php\n\u003c?php\n\nuse CommandString\\Cookies\\CookieController;\nuse Psr\\Http\\Message\\ServerRequestInterface;\nuse React\\Http\\HttpServer;\nuse React\\Http\\Message\\Response;\nuse React\\Socket\\SocketServer;\n\nrequire_once \"vendor/autoload.php\";\n\n$cookies = new CookieController();\n\n$http = new HttpServer(function (ServerRequestInterface $req) use ($cookies) {\n    $res = new React\\Http\\Message\\Response;\n\n    $cookie = $cookies-\u003ecookie($req, $res);\n\n    $parts = explode(\"/\", $req-\u003egetRequestTarget());\n    $partsNum = count($parts) - 1;\n\n    $invalidReq = function (string $message) use (\u0026$res): Response\n    {\n        $res-\u003ewithStatus(403);\n\t\t$res = $res-\u003ewithHeader('content-type', 'text-plain');\n\t\t$res-\u003egetBody()-\u003ewrite($message);\n    };\n\n    if ($parts[1] === \"set\") {\n        if ($partsNum !== 3) {\n            return $invalidReq(\"Invalid URI, example `/set/id/123456`\");\n        }\n\n        $cookie-\u003eset($parts[2], $parts[3]);\n        $res-\u003egetBody()-\u003ewrite(\"Set cookie {$parts[2]} to {$parts[3]}\");\n    }\n\n    if ($parts[1] === \"get\") {\n        if ($partsNum !== 2) {\n            return $invalidReq(\"Invalid URI, example `/get/id`\");\n        }\n\n        if ($cookie-\u003eexists($parts[2])) {\n            $res-\u003egetBody()-\u003ewrite(\"Found cookie {$parts[2]}, it is set to {$cookie-\u003eget($parts[2])}\");\n        } else {\n            $res-\u003egetBody()-\u003ewrite(\"Cookie {$parts[2]} does not exist\");\n        }\n    }\n\n    if ($parts[1] === \"delete\") {\n        if ($partsNum !== 2) {\n            return $invalidReq(\"Invalid URI, example `/delete/id`\");\n        }\n\n        if ($cookie-\u003eexists($parts[2])) {\n            $cookie-\u003edelete($parts[2]);\n            $res-\u003egetBody()-\u003ewrite(\"Deleted cookie {$parts[2]}\");\n        } else {\n            $res-\u003egetBody()-\u003ewrite(\"Cooke {$parts[2]} does not exist\");\n        }\n    }\n    \n    return $res;\n});\n\n$socket = new SocketServer('127.0.0.1:8000');\n$http-\u003elisten($socket);\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcommandstring%2Freactphp-cookies","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcommandstring%2Freactphp-cookies","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcommandstring%2Freactphp-cookies/lists"}