PHPackages                             dflydev/fig-cookies - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. dflydev/fig-cookies

ActiveLibrary[HTTP &amp; Networking](/categories/http)

dflydev/fig-cookies
===================

Cookies for PSR-7 HTTP Message Interface.

v3.2.0(8mo ago)2268.5M—8.2%29[12 issues](https://github.com/dflydev/dflydev-fig-cookies/issues)[3 PRs](https://github.com/dflydev/dflydev-fig-cookies/pulls)20MITPHPPHP ^7.2 || ^8.0CI passing

Since Jun 3Pushed 8mo ago12 watchersCompare

[ Source](https://github.com/dflydev/dflydev-fig-cookies)[ Packagist](https://packagist.org/packages/dflydev/fig-cookies)[ RSS](/packages/dflydev-fig-cookies/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (9)Versions (14)Used By (20)

FIG Cookies
===========

[](#fig-cookies)

Managing Cookies for PSR-7 Requests and Responses.

[![Latest Stable Version](https://camo.githubusercontent.com/85380ff2ee303de4f3e4ea59e97ecf2b21c5872760fe3baad6611408f67c6744/68747470733a2f2f706f7365722e707567782e6f72672f64666c796465762f6669672d636f6f6b6965732f762f737461626c65)](https://packagist.org/packages/dflydev/fig-cookies)[![Total Downloads](https://camo.githubusercontent.com/e1c3ccd289df5ee6551295e57179a451352ecf0dbf7369bb02fbd409e920e67e/68747470733a2f2f706f7365722e707567782e6f72672f64666c796465762f6669672d636f6f6b6965732f646f776e6c6f616473)](https://packagist.org/packages/dflydev/fig-cookies)[![Latest Unstable Version](https://camo.githubusercontent.com/0b0772a2fc017e18aa88f6dcc262fd5dfbe34d0580b1a169ce6c3a9a45e338e2/68747470733a2f2f706f7365722e707567782e6f72672f64666c796465762f6669672d636f6f6b6965732f762f756e737461626c65)](https://packagist.org/packages/dflydev/fig-cookies)[![License](https://camo.githubusercontent.com/d08e62cb11730c35d97d832993204a1ff2e32e980290b2bb5569c18c998e785c/68747470733a2f2f706f7365722e707567782e6f72672f64666c796465762f6669672d636f6f6b6965732f6c6963656e7365)](https://packagist.org/packages/dflydev/fig-cookies)
[![Build Status](https://camo.githubusercontent.com/7ff7d661918d527e52ad0412100824a35b72bf54ae6766afaaa64199d9d435d9/68747470733a2f2f7472617669732d63692e6f72672f64666c796465762f64666c796465762d6669672d636f6f6b6965732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/dflydev/dflydev-fig-cookies)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/1f6780bc824299c8119029c5ce28a59c815bf1564091ce856dadeac485e6e5f5/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f64666c796465762f64666c796465762d6669672d636f6f6b6965732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/dflydev/dflydev-fig-cookies/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/1fb06644a97627310751e45f2944c31b6e98e2ef4c6fcb89bcb2b96cf050cf84/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f64666c796465762f64666c796465762d6669672d636f6f6b6965732f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/dflydev/dflydev-fig-cookies/?branch=master)[![Code Climate](https://camo.githubusercontent.com/306f2b374304447d515941adb98bfa1b405ff956d8e5e8bf427fd46b69748b18/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f64666c796465762f64666c796465762d6669672d636f6f6b6965732f6261646765732f6770612e737667)](https://codeclimate.com/github/dflydev/dflydev-fig-cookies)
[![Join the chat at https://gitter.im/dflydev/dflydev-fig-cookies](https://camo.githubusercontent.com/abe08b740a4156153736f791393ec4da6619c4be73212e75769f52edacc0e2b5/68747470733a2f2f6261646765732e6769747465722e696d2f4a6f696e253230436861742e737667)](https://gitter.im/dflydev/dflydev-fig-cookies?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

Installation
------------

[](#installation)

```
$> composer require dflydev/fig-cookies
```

Concepts
--------

[](#concepts)

FIG Cookies tackles two problems, managing **Cookie** *Request* headers and managing **Set-Cookie** *Response* headers. It does this by way of introducing a `Cookies` class to manage collections of `Cookie` instances and a `SetCookies` class to manage collections of `SetCookie` instances.

Instantiating these collections looks like this:

```
// Get a collection representing the cookies in the Cookie headers
// of a PSR-7 Request.
$cookies = Dflydev\FigCookies\Cookies::fromRequest($request);

// Get a collection representing the cookies in the Set-Cookie headers
// of a PSR-7 Response
$setCookies = Dflydev\FigCookies\SetCookies::fromResponse($response);
```

After modifying these collections in some way, they are rendered into a PSR-7 Request or PSR-7 Response like this:

```
// Render the Cookie headers and add them to the headers of a
// PSR-7 Request.
$request = $cookies->renderIntoCookieHeader($request);

// Render the Set-Cookie headers and add them to the headers of a
// PSR-7 Response.
$response = $setCookies->renderIntoSetCookieHeader($response);
```

Like PSR-7 Messages, `Cookie`, `Cookies`, `SetCookie`, and `SetCookies`are all represented as immutable value objects and all mutators will return new instances of the original with the requested changes.

While this style of design has many benefits it can become fairly verbose very quickly. In order to get around that, FIG Cookies provides two facades in an attempt to help simplify things and make the whole process less verbose.

Basic Usage
-----------

[](#basic-usage)

The easiest way to start working with FIG Cookies is by using the `FigRequestCookies` and `FigResponseCookies` classes. They are facades to the primitive FIG Cookies classes. Their jobs are to make common cookie related tasks easier and less verbose than working with the primitive classes directly.

There is overhead on creating `Cookies` and `SetCookies` and rebuilding requests and responses. Each of the `FigCookies` methods will go through this process so be wary of using too many of these calls in the same section of code. In some cases it may be better to work with the primitive FIG Cookies classes directly rather than using the facades.

### Request Cookies

[](#request-cookies)

Requests include cookie information in the **Cookie** request header. The cookies in this header are represented by the `Cookie` class.

```
use Dflydev\FigCookies\Cookie;

$cookie = Cookie::create('theme', 'blue');
```

To easily work with request cookies, use the `FigRequestCookies` facade.

#### Get a Request Cookie

[](#get-a-request-cookie)

The `get` method will return a `Cookie` instance. If no cookie by the specified name exists, the returned `Cookie` instance will have a `null` value.

The optional third parameter to `get` sets the value that should be used if a cookie does not exist.

```
use Dflydev\FigCookies\FigRequestCookies;

$cookie = FigRequestCookies::get($request, 'theme');
$cookie = FigRequestCookies::get($request, 'theme', 'default-theme');
```

#### Set a Request Cookie

[](#set-a-request-cookie)

The `set` method will either add a cookie or replace an existing cookie.

The `Cookie` primitive is used as the second argument.

```
use Dflydev\FigCookies\FigRequestCookies;

$request = FigRequestCookies::set($request, Cookie::create('theme', 'blue'));
```

#### Modify a Request Cookie

[](#modify-a-request-cookie)

The `modify` method allows for replacing the contents of a cookie based on the current cookie with the specified name. The third argument is a `callable` that takes a `Cookie` instance as its first argument and is expected to return a `Cookie` instance.

If no cookie by the specified name exists, a new `Cookie` instance with a `null` value will be passed to the callable.

```
use Dflydev\FigCookies\FigRequestCookies;

$modify = function (Cookie $cookie) {
    $value = $cookie->getValue();

    // ... inspect current $value and determine if $value should
    // change or if it can stay the same. in all cases, a cookie
    // should be returned from this callback...

    return $cookie->withValue($value);
}

$request = FigRequestCookies::modify($request, 'theme', $modify);
```

#### Remove a Request Cookie

[](#remove-a-request-cookie)

The `remove` method removes a cookie from the request headers if it exists.

```
use Dflydev\FigCookies\FigRequestCookies;

$request = FigRequestCookies::remove($request, 'theme');
```

Note that this does not cause the client to remove the cookie. Take a look at the `SetCookie` class' `expire()` method to do that.

### Response Cookies

[](#response-cookies)

Responses include cookie information in the **Set-Cookie** response header. The cookies in these headers are represented by the `SetCookie` class.

```
use Dflydev\FigCookies\Modifier\SameSite;
use Dflydev\FigCookies\SetCookie;

$setCookie = SetCookie::create('lu')
    ->withValue('Rg3vHJZnehYLjVg7qi3bZjzg')
    ->withExpires('Tue, 15-Jan-2013 21:47:38 GMT')
    ->withMaxAge(500)
    ->rememberForever()
    ->withPath('/')
    ->withDomain('.example.com')
    ->withSecure(true)
    ->withHttpOnly(true)
    ->withSameSite(SameSite::lax())
    ->withPartitioned()
;
```

To easily work with response cookies, use the `FigResponseCookies` facade.

#### Get a Response Cookie

[](#get-a-response-cookie)

The `get` method will return a `SetCookie` instance. If no cookie by the specified name exists, the returned `SetCookie` instance will have a `null`value.

The optional third parameter to `get` sets the value that should be used if a cookie does not exist.

```
use Dflydev\FigCookies\FigResponseCookies;

$setCookie = FigResponseCookies::get($response, 'theme');
$setCookie = FigResponseCookies::get($response, 'theme', 'simple');
```

#### Set a Response Cookie

[](#set-a-response-cookie)

The `set` method will either add a cookie or replace an existing cookie.

The `SetCookie` primitive is used as the second argument.

```
use Dflydev\FigCookies\FigResponseCookies;

$response = FigResponseCookies::set($response, SetCookie::create('token')
    ->withValue('a9s87dfz978a9')
    ->withDomain('example.com')
    ->withPath('/firewall')
);
```

#### Modify a Response Cookie

[](#modify-a-response-cookie)

The `modify` method allows for replacing the contents of a cookie based on the current cookie with the specified name. The third argument is a `callable` that takes a `SetCookie` instance as its first argument and is expected to return a `SetCookie` instance.

If no cookie by the specified name exists, a new `SetCookie` instance with a `null` value will be passed to the callable.

```
use Dflydev\FigCookies\FigResponseCookies;

$modify = function (SetCookie $setCookie) {
    $value = $setCookie->getValue();

    // ... inspect current $value and determine if $value should
    // change or if it can stay the same. in all cases, a cookie
    // should be returned from this callback...

    return $setCookie
        ->withValue($newValue)
        ->withExpires($newExpires)
    ;
}

$response = FigResponseCookies::modify($response, 'theme', $modify);
```

#### Remove a Response Cookie

[](#remove-a-response-cookie)

The `remove` method removes a cookie from the response if it exists.

```
use Dflydev\FigCookies\FigResponseCookies;

$response = FigResponseCookies::remove($response, 'theme');
```

#### Expire a Response Cookie

[](#expire-a-response-cookie)

The `expire` method sets a cookie with an expiry date in the far past. This causes the client to remove the cookie.

Note that in order to expire a cookie, you need to configure its `Set-Cookie`header just like when you initially wrote the cookie (i.e. same domain/path). The easiest way to do this is to re-use the same code for configuring the header when setting as well as expiring the cookie:

```
use Dflydev\FigCookies\FigResponseCookies;
use Dflydev\FigCookies\SetCookie;

$setCookie = SetCookie::create('ba')
    ->withValue('UQdfdafpJJ23k111m')
    ->withPath('/')
    ->withDomain('.example.com')
;

FigResponseCookies::set($response, $setCookie->expire());
```

FAQ
---

[](#faq)

### Do you call `setcookies`?

[](#do-you-call-setcookies)

No.

Delivery of the rendered `SetCookie` instances is the responsibility of the PSR-7 client implementation.

### Do you do anything with sessions?

[](#do-you-do-anything-with-sessions)

No.

It would be possible to build session handling using cookies on top of FIG Cookies but it is out of scope for this package.

### Do you read from `$_COOKIES`?

[](#do-you-read-from-_cookies)

No.

FIG Cookies only pays attention to the `Cookie` headers on [PSR-7](https://packagist.org/packages/psr/http-message) Request instances. In the case of `ServerRequestInterface` instances, PSR-7 implementations should be including `$_COOKIES` values in the headers so in that case FIG Cookies may be interacting with `$_COOKIES`indirectly.

License
-------

[](#license)

MIT, see LICENSE.

Community
---------

[](#community)

Want to get involved? Here are a few ways:

- Find us in the #dflydev IRC channel on irc.freenode.org.
- Mention [@dflydev](https://twitter.com/dflydev) on Twitter.
- [![Join the chat at https://gitter.im/dflydev/dflydev-fig-cookies](https://camo.githubusercontent.com/abe08b740a4156153736f791393ec4da6619c4be73212e75769f52edacc0e2b5/68747470733a2f2f6261646765732e6769747465722e696d2f4a6f696e253230436861742e737667)](https://gitter.im/dflydev/dflydev-fig-cookies?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

###  Health Score

62

—

FairBetter than 99% of packages

Maintenance58

Moderate activity, may be stable

Popularity63

Solid adoption and visibility

Community42

Growing community involvement

Maturity75

Established project with proven stability

 Bus Factor1

Top contributor holds 53.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~340 days

Recently: every ~432 days

Total

12

Last Release

257d ago

Major Versions

1.0.x-dev → v2.0.02018-07-11

v2.0.3 → v3.0.02021-01-22

PHP version history (3 changes)v1.0.0PHP &gt;=5.4

v2.0.0PHP ^7.2

v3.0.0PHP ^7.2 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/0d6029bd16a0e8a9b97358a75b54affa6baadc1d9b0ca7eab2b6dd6c681ad7a8?d=identicon)[simensen](/maintainers/simensen)

---

Top Contributors

[![simensen](https://avatars.githubusercontent.com/u/191200?v=4)](https://github.com/simensen "simensen (59 commits)")[![Ocramius](https://avatars.githubusercontent.com/u/154256?v=4)](https://github.com/Ocramius "Ocramius (18 commits)")[![gsteel](https://avatars.githubusercontent.com/u/2803720?v=4)](https://github.com/gsteel "gsteel (9 commits)")[![battis](https://avatars.githubusercontent.com/u/419619?v=4)](https://github.com/battis "battis (8 commits)")[![franzliedke](https://avatars.githubusercontent.com/u/249125?v=4)](https://github.com/franzliedke "franzliedke (5 commits)")[![harikt](https://avatars.githubusercontent.com/u/120454?v=4)](https://github.com/harikt "harikt (3 commits)")[![canvural](https://avatars.githubusercontent.com/u/1574232?v=4)](https://github.com/canvural "canvural (2 commits)")[![pavlakis](https://avatars.githubusercontent.com/u/673223?v=4)](https://github.com/pavlakis "pavlakis (1 commits)")[![proton-ab](https://avatars.githubusercontent.com/u/25139420?v=4)](https://github.com/proton-ab "proton-ab (1 commits)")[![schnittstabil](https://avatars.githubusercontent.com/u/6059032?v=4)](https://github.com/schnittstabil "schnittstabil (1 commits)")[![settermjd](https://avatars.githubusercontent.com/u/196801?v=4)](https://github.com/settermjd "settermjd (1 commits)")[![weierophinney](https://avatars.githubusercontent.com/u/25943?v=4)](https://github.com/weierophinney "weierophinney (1 commits)")[![mbakker96](https://avatars.githubusercontent.com/u/10044261?v=4)](https://github.com/mbakker96 "mbakker96 (1 commits)")[![MrDys](https://avatars.githubusercontent.com/u/123333?v=4)](https://github.com/MrDys "MrDys (1 commits)")

---

Tags

psr-7psr7cookies

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/dflydev-fig-cookies/health.svg)

```
[![Health](https://phpackages.com/badges/dflydev-fig-cookies/health.svg)](https://phpackages.com/packages/dflydev-fig-cookies)
```

###  Alternatives

[league/route

Fast routing and dispatch component including PSR-15 middleware, built on top of FastRoute.

6633.1M115](/packages/league-route)[neomerx/cors-psr7

Framework agnostic (PSR-7) CORS implementation (www.w3.org/TR/cors/)

682.4M19](/packages/neomerx-cors-psr7)[phpro/http-tools

HTTP tools for developing more consistent HTTP implementations.

28137.8k](/packages/phpro-http-tools)[wellrested/wellrested

Simple PHP Library for RESTful APIs

4818.7k4](/packages/wellrested-wellrested)[mtymek/blast-base-url

PSR-7 middleware and helpers for working with base URL.

1054.0k3](/packages/mtymek-blast-base-url)[prooph/http-middleware

http middleware for prooph components

1145.0k](/packages/prooph-http-middleware)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
