PHPackages                             stefna/cookie - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. stefna/cookie

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

stefna/cookie
=============

Cookie management for psr7/psr15

1.0.0(2y ago)01.2kMITPHPPHP ^8.2

Since Oct 25Pushed 2y ago3 watchersCompare

[ Source](https://github.com/stefna/cookie)[ Packagist](https://packagist.org/packages/stefna/cookie)[ RSS](/packages/stefna-cookie/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (15)Versions (2)Used By (0)

Cookies for psr-15 and psr-7
============================

[](#cookies-for-psr-15-and-psr-7)

[![Build Status](https://github.com/stefna/cookie/actions/workflows/continuous-integration.yml/badge.svg?branch=main)](https://github.com/stefna/cookie/actions/workflows/continuous-integration.yml)[![Latest Version on Packagist](https://camo.githubusercontent.com/faf54187ac6f2f3e8d5b3d259bc81151796a11e2d0c89f3b5c0e8c36b4f71693/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f737465666e612f636f6f6b69652e737667)](https://packagist.org/packages/stefna/cookie)[![Software License](https://camo.githubusercontent.com/7238aa2436e0f9f428aa9ae134c0f4403f4897548de119713ce8266d87451cd1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f737465666e612f636f6f6b69652e737667)](LICENSE)

The package helps in working with HTTP cookies in a PSR-7/PSR-15 environment.

Requirements
------------

[](#requirements)

PHP 8.2 or higher.

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

[](#installation)

```
composer require stefna/cookie
```

Motivation
----------

[](#motivation)

When using PSR-7 the `setcookie()` function becomes useless when you want everything connected to the request-response loop to be in the Request and Response objects.

`setcookie()` also have a tendency to not support the latest features of the http cookie standard.

Concept
-------

[](#concept)

The main concept of package is the separation of read/write of cookie from using the cookie so with that in mind the `CookieJar` is not connected to the http layer it's just a container that stores cookies.

It's then up to the `CookieMiddleware` and `CookieManager` to read and write the http headers.

By separating it like this it's easy to implement Signing or encryption of the cookies since it's not part of the code that uses the cookies.

It also means you don't need the Response object when creating new cookies all you need to do is add the cookie to the `CookieJar` and the middleware deals with writing the cookie

Usage
-----

[](#usage)

Usage of cookie jar from inside action.

The `CookieJar` is added to the request by the `CookieMiddleware` that middleware also deals with adding the cookies to the response headers.

```
use Psr\Http\Message\ServerRequestInterface;

class Action
{
	public function __invoke(ServerRequestInterface $request)
	{
		$cookieJar = $request->getAttribute(\Stefna\Cookie\CookieJar::class);

		if (!$cookieJar->has('visits')) {
			$cookieJar->set(new \Stefna\Cookie\Cookie('visits', 0));
		}
		$visitCookie = $cookieJar->get('visits');
		if ($cookieJar->getInt('visits') < 10) {
			$cookieJar->set($visitCookie
				->withValue($cookieJar->getInt('visits') + 1)
				->withExpires(DateInterval::createFromDateString('+1 week'))
			);
		}
		else {
			// remove cookie alt 1
			$cookieJar->set($visitCookie->expire());
			// remove cookie alt 2
			$cookieJar->remove($visitCookie);
		}
	}
}
```

Contribute
----------

[](#contribute)

We are always happy to receive bug/security reports and bug/security fixes

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE) for more information.

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

982d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ce9d40996574fd1f18f69556c872c6eb94e59977c5f0b33c9df541d82fbb3800?d=identicon)[sunkan](/maintainers/sunkan)

---

Top Contributors

[![sunkan](https://avatars.githubusercontent.com/u/568492?v=4)](https://github.com/sunkan "sunkan (21 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/stefna-cookie/health.svg)

```
[![Health](https://phpackages.com/badges/stefna-cookie/health.svg)](https://phpackages.com/packages/stefna-cookie)
```

###  Alternatives

[symfony/symfony

The Symfony PHP framework

31.4k87.2M2.2k](/packages/symfony-symfony)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[flow-php/flow

PHP ETL - Extract Transform Load - Data processing framework

85036.3k](/packages/flow-php-flow)[cakephp/cakephp

The CakePHP framework

8.9k19.5M1.8k](/packages/cakephp-cakephp)[mcp/sdk

Model Context Protocol SDK for Client and Server applications in PHP

1.5k1.5M88](/packages/mcp-sdk)[ecotone/ecotone

Enterprise architecture layer for Laravel and Symfony — CQRS, Event Sourcing, Durable Workflows (Sagas, Orchestrators), Projections, and Outbox messaging via PHP attributes.

564576.7k52](/packages/ecotone-ecotone)

PHPackages © 2026

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