# Cookie Handler or Middleware for Slim 3

**URL:** <https://discourse.slimframework.com/t/cookie-handler-or-middleware-for-slim-3/4131>\
**Category:** Questions\
**Created:** [May 4, 2020, 10:29am UTC](https://discourse.slimframework.com/t/cookie-handler-or-middleware-for-slim-3/4131 "2020-05-04T10:29:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![raziul](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/raziul/32/1161_2.png) [@raziul](https://discourse.slimframework.com/u/raziul)\
**Post date:** [May 4, 2020, 10:29am UTC](https://discourse.slimframework.com/t/cookie-handler-or-middleware-for-slim-3/4131/1 "2020-05-04T10:29:56Z")

</div>

I was looking inside Slim 3 and found Slim\Http\Cookies class which may be intended to use in Slim 3 but it never completed and neither mentioned by anyone.  
Recently I looked for cookie handler and many of you suggested for [dflydev/fig-cookies](https://github.com/dflydev/dflydev-fig-cookies) but I think this library doesn’t work in PHP 7.0 and that’s a big issue to me as I am making applications that run on PHP 7.0 or later.

I would like to get suggestions from you for a good library that works in PHP7.0 otherwise I have to create a new one.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [May 4, 2020, 11:56am UTC](https://discourse.slimframework.com/t/cookie-handler-or-middleware-for-slim-3/4131/2 "2020-05-04T11:56:29Z")

</div>

Vanilla PHP Cookies of course work always and everywhere. I’m joking 😉

The most cookie libraries I know are not good maintained anymore or too complex…  
except the Symfony Http Foundation library. This library works also very well with Sessions in Slim.

```auto
use Symfony\Component\HttpFoundation\Cookie;  
$cookie = new Cookie('color', 'green', strtotime('tomorrow'), '/', 
   'somedomain.com', true, true);

```

---

<div class="post-metadata">

**Author:** ![raziul](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/raziul/32/1161_2.png) [@raziul](https://discourse.slimframework.com/u/raziul)\
**Post date:** [May 4, 2020, 12:10pm UTC](https://discourse.slimframework.com/t/cookie-handler-or-middleware-for-slim-3/4131/3 "2020-05-04T12:10:39Z")

</div>

What about samesite in setcookie()??

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [May 4, 2020, 12:21pm UTC](https://discourse.slimframework.com/t/cookie-handler-or-middleware-for-slim-3/4131/4 "2020-05-04T12:21:52Z")

</div>

> What about samesite in setcookie()??

I think “native” samesite cookies works only with PHP 7.3+.

Maybe you try this with Slim under PHP 7.0:

```php
use Symfony\Component\HttpFoundation\Cookie;

//...

$response = $handler->handle($request);

$cookie = new Cookie(
    'color',
    'green',
    strtotime('tomorrow'),
    '/',
    null,
    true,
    true,
    false,
    Cookie::SAMESITE_LAX
);

$response = $response->withAddedHeader('Set-Cookie', (string)$cookie);

return $response;

```

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/2X/9/9405c451f652e120b0cfa2f81c4ebb01ea6f6175.png)

---

<div class="post-metadata">

**Author:** ![raziul](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/raziul/32/1161_2.png) [@raziul](https://discourse.slimframework.com/u/raziul)\
**Post date:** [May 4, 2020, 12:28pm UTC](https://discourse.slimframework.com/t/cookie-handler-or-middleware-for-slim-3/4131/5 "2020-05-04T12:28:58Z")

</div>

I am trying to use middleware to update the response.  
I am using a different approach where a cookie class holds the value and update it using middleware
