PHPackages                             tobento/service-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. [HTTP &amp; Networking](/categories/http)
4. /
5. tobento/service-cookie

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

tobento/service-cookie
======================

Http cookie for PHP applications.

2.0(7mo ago)01671MITPHPPHP &gt;=8.4

Since Jun 10Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/tobento-ch/service-cookie)[ Packagist](https://packagist.org/packages/tobento/service-cookie)[ Docs](https://www.tobento.ch)[ RSS](/packages/tobento-service-cookie/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (3)Dependencies (11)Versions (5)Used By (1)

Cookie Service
==============

[](#cookie-service)

The cookie service provides a way for managing cookies for PHP applications.

Table of Contents
-----------------

[](#table-of-contents)

- [Getting started](#getting-started)
    - [Requirements](#requirements)
    - [Highlights](#highlights)
- [Documentation](#documentation)
    - [Basic Usage](#basic-usage)
        - [Using Middleware](#using-middleware)
    - [Read Cookie](#read-cookie)
        - [Cookie Values Factory](#cookie-values-factory)
        - [Cookie Values](#cookie-values)
    - [Write Cookie](#write-cookie)
        - [Cookies Factory](#cookies-factory)
        - [Cookies](#cookies)
        - [Cookie Factory](#cookie-factory)
        - [Cookie](#cookie)
    - [Cookies Processor](#cookies-processor)
- [Credits](#credits)

---

Getting started
===============

[](#getting-started)

Add the latest version of the cookie service project running this command.

```
composer require tobento/service-cookie

```

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

[](#requirements)

- PHP 8.4 or greater

Highlights
----------

[](#highlights)

- Framework-agnostic, will work with any project
- Decoupled design

Documentation
=============

[](#documentation)

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

[](#basic-usage)

Simple example using the cookie service in your application:

```
use Tobento\Service\Cookie;
use Tobento\Service\Encryption\EncrypterInterface;

// Set up cookie values for reading cookies:
$cookieValues = new Cookie\CookieValues($_COOKIE);

// You may use the default processor for decryption:
$processor = new Cookie\CookiesProcessor(
    encrypter: null, // null|EncrypterInterface
    whitelistedCookies: ['PHPSESSID'],
);

$cookieValues = $processor->processCookieValues($cookieValues);

// Start reading values:
$value = $cookieValues->get('foo');

// Set up Cookies for writing:
$cookies = new Cookie\Cookies(
    cookieFactory: new Cookie\CookieFactory(),
);

// Adding cookies:
$cookies->add('name', 'value');

// You may use the default processor for encryption:
$cookies = $processor->processCookies($cookies);

// Send cookies before any header is sent:
foreach($cookies as $cookie) {
    $cookie->send();
}
```

### Using Middleware

[](#using-middleware)

You may prefer to use the `Cookies` middlware:

```
use Psr\Http\Server\MiddlewareInterface;
use Tobento\Service\Cookie\Middleware;
use Tobento\Service\Cookie\CookieValuesFactory;
use Tobento\Service\Cookie\CookiesFactory;
use Tobento\Service\Cookie\CookieFactory;
use Tobento\Service\Cookie\CookiesProcessor;
use Tobento\Service\Cookie\CookieValuesFactoryInterface;
use Tobento\Service\Cookie\CookiesFactoryInterface;
use Tobento\Service\Cookie\CookiesProcessorInterface;

$middleware = new Middleware\Cookies(
    // CookieValuesFactoryInterface
    cookieValuesFactory: new CookieValuesFactory(),

    // CookiesFactoryInterface
    cookiesFactory: new CookiesFactory(new CookieFactory()),

    // CookiesProcessorInterface
    cookiesProcessor: new CookiesProcessor(),
);

var_dump($middleware instanceof MiddlewareInterface);
// bool(true)
```

**Read and write cookies**

```
use Psr\Http\Message\ServerRequestInterface;
use Tobento\Service\Cookie\CookieValuesInterface;
use Tobento\Service\Cookie\CookiesInterface;

// ...

public function index(ServerRequestInterface $request): void
{
    // read cookies:
    $cookieValues = $request->getAttribute(CookieValuesInterface::class);

    $value = $cookieValues->get('foo');

    // or
    var_dump($request->getCookieParams());

    // write cookies:
    $cookies = $request->getAttribute(CookiesInterface::class);

    $cookies->add('name', 'value');
}
```

Read Cookie
-----------

[](#read-cookie)

### Cookie Values Factory

[](#cookie-values-factory)

You may use the `CookieValuesFactory` to create cookie values:

**createCookieValuesFromArray**

```
use Tobento\Service\Cookie\CookieValuesFactory;
use Tobento\Service\Cookie\CookieValuesFactoryInterface;
use Tobento\Service\Cookie\CookieValuesInterface;

$factory = new CookieValuesFactory();

var_dump($factory instanceof CookieValuesFactoryInterface);
// bool(true)

$cookieValues = $factory->createCookieValuesFromArray($_COOKIE);

var_dump($cookieValues instanceof CookieValuesInterface);
// bool(true)
```

You may check out the [Cookie Values](#cookie-values) to learn more about it.

**createCookieValuesFromCookies**

```
use Tobento\Service\Cookie\CookieValuesFactory;
use Tobento\Service\Cookie\CookiesInterface;

$factory = new CookieValuesFactory();

$cookieValues = $factory->createCookieValuesFromCookies(
    $cookies // CookiesInterface
);
```

You may check out the [Cookie Values](#cookie-values) to learn more about it.

You may check out the [Cookies](#cookies) to learn more about it.

### Cookie Values

[](#cookie-values)

```
use Tobento\Service\Cookie\CookieValues;
use Tobento\Service\Cookie\CookieValuesInterface;

$cookieValues = new CookieValues(['name' => 'value']);

var_dump($cookieValues instanceof CookieValuesInterface);
// bool(true)
```

**get**

Get a cookie value by name.

```
use Tobento\Service\Cookie\CookieValues;

$values = new CookieValues([
    'name' => 'value',
    'meta' => [
        'color' => 'red',
    ],
]);

var_dump($values->get(name: 'name'));
// string(5) "value"

// supports array access:
var_dump($values['name']);

// supports dot notation:
var_dump($values->get(name: 'meta.color'));
// string(3) "red"

// with a default if not exists:
var_dump($values->get(name: 'foo', default: 'value'));
// string(5) "value"

var_dump($values->get(name: 'foo'));
// NULL
```

**has**

Check if a cookie value by name exists.

```
use Tobento\Service\Cookie\CookieValues;

$values = new CookieValues([
    'name' => 'value',
    'meta' => [
        'color' => 'red',
    ],
]);

var_dump($values->has(name: 'name'));
// bool(true)

// supports dot notation:
var_dump($values->has(name: 'meta.color'));
// bool(true)

var_dump($values->has(name: 'foo'));
// bool(false)
```

**all**

Get a cookie values.

```
use Tobento\Service\Cookie\CookieValues;

$values = new CookieValues([
    'name' => 'value',
    'meta' => [
        'color' => 'red',
    ],
]);

var_dump($values->all());
// array(2) {["name"]=> string(5) ... }

// or just
foreach($values as $value) {}
```

**map**

Map over each of the cookie values returning a new instance.

```
use Tobento\Service\Cookie\CookieValues;

$values = new CookieValues([
    'name' => 'value',
]);

$valuesNew = $values->map(function(mixed $value, string|int $name): mixed {
    return $value;
});
```

**withValues**

Returns a new instance with the specified values.

```
use Tobento\Service\Cookie\CookieValues;

$values = new CookieValues([
    'name' => 'value',
]);

$valuesNew = $values->withValues(['name' => 'value']);
```

Write Cookie
------------

[](#write-cookie)

### Cookies Factory

[](#cookies-factory)

You may use the `CookiesFactory` to create cookies:

**createCookies**

```
use Tobento\Service\Cookie\CookiesFactory;
use Tobento\Service\Cookie\CookiesFactoryInterface;
use Tobento\Service\Cookie\CookieFactory;
use Tobento\Service\Cookie\CookiesInterface;
use Tobento\Service\Cookie\CookieInterface;
use Tobento\Service\Cookie\Cookie;

$cookiesFactory = new CookiesFactory(
    cookieFactory: new CookieFactory(),
);

var_dump($cookiesFactory instanceof CookiesFactoryInterface);
// bool(true)

$cookies = $cookiesFactory->createCookies();

var_dump($cookies instanceof CookiesInterface);
// bool(true)

$cookies = $cookiesFactory->createCookies(
    new Cookie(name: 'name', value: 'value'),
);
```

You may check out the [Cookie Factory](#cookie-factory) to learn more about it.

You may check out the [Cookies](#cookies) to learn more about it.

**createCookiesFromKeyValuePairs**

Create new cookies from key/value pairs. May be used for creating cookies from the `$_COOKIE` superglobal.

```
use Tobento\Service\Cookie\CookiesFactory;
use Tobento\Service\Cookie\CookieFactory;
use Tobento\Service\Cookie\CookiesInterface;

$cookiesFactory = new CookiesFactory(
    cookieFactory: new CookieFactory(),
);

$cookies = $cookiesFactory->createCookiesFromKeyValuePairs([
    'name' => 'value',
]);

var_dump($cookies instanceof CookiesInterface);
// bool(true)
```

You may check out the [Cookie Factory](#cookie-factory) to learn more about it.

You may check out the [Cookies](#cookies) to learn more about it.

**createCookiesFromArray**

```
use Tobento\Service\Cookie\CookiesFactory;
use Tobento\Service\Cookie\CookieFactory;
use Tobento\Service\Cookie\CookiesInterface;

$cookiesFactory = new CookiesFactory(
    cookieFactory: new CookieFactory(),
);

$cookies = $cookiesFactory->createCookiesFromArray([
    [
        'name' => 'name',
        'value' => 'value',

        // The duration in seconds until the cookie will expire.
        'lifetime' => 3600,

        'path' => '/',
        'domain' => '.example.com',
        'secure' => true,
        'httpOnly' => true,
        'sameSite' => 'Lax',
    ],
]);

var_dump($cookies instanceof CookiesInterface);
// bool(true)
```

You may check out the [Cookie Factory](#cookie-factory) to learn more about it.

You may check out the [Cookies](#cookies) to learn more about it.

### Cookies

[](#cookies)

```
use Tobento\Service\Cookie\Cookies;
use Tobento\Service\Cookie\CookiesInterface;
use Tobento\Service\Cookie\CookieFactory;

$cookies = new Cookies(
    cookieFactory: new CookieFactory(),
);

var_dump($cookies instanceof CookiesInterface);
// bool(true)
```

You may check out the [Cookie Factory](#cookie-factory) to learn more about it.

**addCookie**

Add a cookie object.

```
use Tobento\Service\Cookie\Cookies;
use Tobento\Service\Cookie\CookieFactory;
use Tobento\Service\Cookie\Cookie;

$cookies = new Cookies(
    cookieFactory: new CookieFactory(),
);

$cookies->addCookie(
    new Cookie(name: 'name', value: 'value')
);
```

You may check out the [Cookie Factory](#cookie-factory) to learn more about it.

**add**

Add a cookie.

```
use Tobento\Service\Cookie\Cookies;
use Tobento\Service\Cookie\CookieFactory;

$cookies = new Cookies(
    cookieFactory: new CookieFactory(),
);

$cookies->add(
    name: 'name', // string
    value: 'value', // string

    // The duration in seconds until the cookie will expire.
    lifetime: 3600, // null|int

    // if null (default) it uses default value from factory.
    path: '/', // null|string

    // if null (default) it uses default value from factory.
    domain: 'example.com', // null|string

    // if null (default) it uses default value from factory.
    secure: true, // null|bool

    httpOnly: true, // default true if not set

    // if null (default) it uses default value from factory.
    sameSite: 'Lax', // string
);
```

You may change the default values by a custom `CookieFactory::class`.

You may check out the [Cookie Factory](#cookie-factory) to learn more about it.

**get**

Returns a cookie by the specified parameters or null if not found.

```
$cookie = $cookies->get(name: 'name');

// by name and path:
$cookie = $cookies->get(name: 'name', path: 'path');

// by name and domain:
$cookie = $cookies->get(name: 'name', domain: 'example.com');

// by name, path and domain:
$cookie = $cookies->get(name: 'name', path: 'path', domain: 'example.com');
```

**clear**

Clear a cookie by the specified parameters.

```
// clears all with the same name:
$cookies->clear(name: 'name');

// clears only with same name and path:
$cookies->clear(name: 'name', path: 'path');

// clears only with same name and domain:
$cookies->clear(name: 'name', domain: 'example.com');

// clears only with same name, path and domain:
$cookies->clear(name: 'name', path: 'path', domain: 'example.com');
```

**column**

Sometimes you may need only specific columns from the cookies returning an `array`.

```
$names = $cookies->column('name');

$values = $cookies->column('value');

// values keyed by name:
$values = $cookies->column('value', 'name');
```

**first**

Returns the first cookie, otherwise null.

```
use Tobento\Service\Cookie\CookieInterface;

$cookie = $cookies->first();

var_dump($cookie instanceof CookieInterface);
// bool(true) or NULL
```

**all**

Returns all cookies.

```
use Tobento\Service\Cookie\CookieInterface;

$cookies = $cookies->all();
// array

// or just
foreach($cookies as $cookie) {}
```

**filter**

Returns a new instance with the filtered cookies.

```
use Tobento\Service\Cookie\CookieInterface;

$cookiesNew = $cookies->filter(
    fn(CookieInterface $c): bool => $c->name() === 'foo'
);
```

**name**

Returns a new instance with the name filtered.

```
$cookiesNew = $cookies->name('foo');
```

**path**

Returns a new instance with the path filtered.

```
$cookiesNew = $cookies->path('/');
```

**domain**

Returns a new instance with the domain filtered.

```
$cookiesNew = $cookies->domain('example.com');
```

**map**

Map over each of the cookies returning a new instance.

```
use Tobento\Service\Cookie\CookieInterface;

$cookiesNew = $cookies->map(function(CookieInterface $c): CookieInterface {
    return $c;
});
```

**toHeader**

Returns the cookie header.

```
var_dump($cookies->toHeader());
// array(1) { [0]=> string(127) "name=value; Expires=Tuesday, 06-Jun-2023 18:34:46 GMT; Max-Age=3600; Path=/; Domain=example.com; Secure; HttpOnly; SameSite=Lax" }
```

### Cookie Factory

[](#cookie-factory)

You may use the `CookieFactory` to create a cookie:

**createCookie**

```
use Tobento\Service\Cookie\CookieFactory;
use Tobento\Service\Cookie\CookieFactoryInterface;
use Tobento\Service\Cookie\CookieInterface;

$cookieFactory = new CookieFactory(
    // default values:
    path: '/',
    domain: '',
    secure: true,
    sameSite: 'Lax',
);

var_dump($cookieFactory instanceof CookieFactoryInterface);
// bool(true)

$cookie = $cookieFactory->createCookie(
    name: 'name', // string
    value: 'value', // string

    // The duration in seconds until the cookie will expire.
    lifetime: 3600, // null|int

    // if null (default) it uses default value.
    path: '/', // null|string

    // if null (default) it uses default value.
    domain: 'example.com', // null|string

    // if null (default) it uses default value.
    secure: true, // null|bool

    httpOnly: true, // default true if not set

    // if null (default) it uses default value.
    sameSite: 'Lax', // string
);

var_dump($cookie instanceof CookieInterface);
// bool(true)
```

You may check out the [Cookie](#cookie) to learn more about it.

**createCookieFromArray**

```
use Tobento\Service\Cookie\CookieFactory;
use Tobento\Service\Cookie\CookieInterface;

$cookieFactory = new CookieFactory(
    // default values:
    path: '/',
    domain: '',
    secure: true,
    sameSite: 'Lax',
);

$cookie = $cookieFactory->createCookieFromArray([
    'name' => 'name',
    'value' => 'value',

    // The duration in seconds until the cookie will expire.
    'lifetime' => 3600,

    'path' => '/',
    'domain' => '.example.com',
    'secure' => true,
    'httpOnly' => true,
    'sameSite' => 'Lax',
]);

var_dump($cookie instanceof CookieInterface);
// bool(true)
```

You may check out the [Cookie](#cookie) to learn more about it.

### Cookie

[](#cookie)

```
use Tobento\Service\Cookie\Cookie;
use Tobento\Service\Cookie\CookieInterface;
use Tobento\Service\Cookie\SameSite;
use Tobento\Service\Cookie\SameSiteInterface;

$cookie = new Cookie(
    name: 'name', // string
    value: 'value', // string

    // The duration in seconds until the cookie will expire.
    lifetime: 3600, // null|int

    path: '/', // string

    domain: '', // string

    secure: true, // bool

    httpOnly: true, // bool

    sameSite: new SameSite(value: 'Lax'), // null|SameSiteInterface
);

var_dump($cookie instanceof CookieInterface);
// bool(true)

var_dump($cookie->name());
// string(4) "name"

var_dump($cookie->value());
// string(5) "value"

var_dump($cookie->lifetime());
// int(3600) or NULL

var_dump($cookie->path());
// string(1) "/"

var_dump($cookie->domain());
// string(0) ""

var_dump($cookie->secure());
// bool(true)

var_dump($cookie->httpOnly());
// bool(true)

var_dump($cookie->sameSite());
// null|SameSiteInterface

var_dump($cookie->sameSite()?->value());
// string(3) "Lax"

var_dump($cookie->expires());
// int(1686155135) or NULL

var_dump($cookie->toHeader());
// string(109) "name=value; Expires=Wednesday, 07-Jun-2023 16:27:47 GMT; Max-Age=3600; Path=/; Secure; HttpOnly; SameSite=Lax"

// send the cookie uses setcookie() method:
var_dump($cookie->send());
// bool(true) on success, otherwise false
```

Cookies Processor
-----------------

[](#cookies-processor)

You may use the default cookies processor for encrypting and decrypting cookie values or you may create a custom processor suiting your needs.

```
use Tobento\Service\Cookie\CookiesProcessor;
use Tobento\Service\Cookie\CookiesProcessorInterface;
use Tobento\Service\Encryption\EncrypterInterface;

$processor = new CookiesProcessor(
    encrypter: null, // null|EncrypterInterface
    whitelistedCookies: ['PHPSESSID'],
);

var_dump($processor instanceof CookiesProcessorInterface);
// bool(true)
```

Check out the [Encryption Service](https://github.com/tobento-ch/service-encryption) to learn more about it.

**processCookieValues**

Decrypting the cookie values.

```
use Tobento\Service\Cookie\CookieValuesInterface;

$cookieValues = $processor->processCookieValues(
    cookieValues: $cookieValues // CookieValuesInterface
);
```

You may check out the [Cookie Values](#cookie-values) to learn more about it.

**processCookies**

Encrypting the cookie values.

```
use Tobento\Service\Cookie\CookiesInterface;

$cookieValues = $processor->processCookies(
    cookies: $cookies // CookiesInterface
);
```

You may check out the [Cookies](#cookies) to learn more about it.

**whitelistCookie**

You may use the `whitelistCookie` to add a cookie the whitelist meaning no encryption/decryption is done.

```
$processor->whitelistCookie(name: 'foo');
```

**whitelistedCookies**

Returns the whitelisted cookies.

```
$whitelistedCookies = $processor->whitelistedCookies();
// array
```

Credits
=======

[](#credits)

- [Tobias Strub](https://www.tobento.ch)
- [All Contributors](../../contributors)

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance63

Regular maintenance activity

Popularity14

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity64

Established project with proven stability

 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

Every ~210 days

Total

5

Last Release

225d ago

Major Versions

1.x-dev → 2.02025-09-26

PHP version history (2 changes)1.0.0PHP &gt;=8.0

2.0PHP &gt;=8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/055d6a1b5c2384bb179c75ab0b55914231d898fdc4dffeb30770f81200e52206?d=identicon)[TOBENTOch](/maintainers/TOBENTOch)

---

Top Contributors

[![tobento-ch](https://avatars.githubusercontent.com/u/16684832?v=4)](https://github.com/tobento-ch "tobento-ch (8 commits)")

---

Tags

httpphppackagecookietobento

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[hannesvdvreken/guzzle-debugbar

A Guzzle middleware that logs requests to debugbar's timeline

76410.4k1](/packages/hannesvdvreken-guzzle-debugbar)

PHPackages © 2026

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