# Recommended way to set, get, remove cookies?

**URL:** <https://discourse.slimframework.com/t/recommended-way-to-set-get-remove-cookies/3971>\
**Category:** Questions\
**Created:** [March 28, 2020, 1:18am UTC](https://discourse.slimframework.com/t/recommended-way-to-set-get-remove-cookies/3971 "2020-03-28T01:18:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabswt](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/fabswt/32/1110_2.png) [@fabswt](https://discourse.slimframework.com/u/fabswt)\
**Post date:** [March 28, 2020, 1:18am UTC](https://discourse.slimframework.com/t/recommended-way-to-set-get-remove-cookies/3971/1 "2020-03-28T01:18:48Z")

</div>

Hi there!  
Believe it or not, but I’m upgrading from Slim 2. (Still in the process of upgrading an app to v3… Hoping to get to v4 tomorrow.)

Cookies have been a pain point:

- Slim 2 documented how to do it, and it was very straightforward; nice!
- Slim 3 said to use dflydev/dflydev-fig-cookies, but I’ve wasted 2-3 hours on it today, and it’s bad. The documentation is terrible and, even though the project _looks_ active, their remove method has been broken for over 3 years now.
- Slim 4 doesn’t mention anything on the topic.

Is there an official recommended way to do it, from the Slim dev team?  
If not… does anyone know how people typically do it? Should I just rely on PHP’s built-in functions? (Still gotta read PSR-7.)

Thanks for the help, it’s appreciated.

---

<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:** [March 28, 2020, 11:11am UTC](https://discourse.slimframework.com/t/recommended-way-to-set-get-remove-cookies/3971/2 "2020-03-28T11:11:04Z")

</div>

Hi @fabswt

> Is there an official recommended way to do it, from the Slim dev team?

I don’t know.

> If not… does anyone know how people typically do it? Should I just rely on PHP’s built-in functions?

I would recommend to use a OOP abstraction for the built-in functions in combination with a Session middleware. Here is a fine collection of session libraries for Slim 4:

- [GitHub - laminas/laminas-session: Object-oriented interface to PHP sessions and storage](https://github.com/laminas/laminas-session)
- [GitHub - odan/session: A middleware oriented session handler for PHP and Slim 4+](https://github.com/odan/session#slim-4-integration)
- [GitHub - bryanjhv/slim-session: A very simple session middleware for Slim Framework 2/3/4.](https://github.com/bryanjhv/slim-session)

---

<div class="post-metadata">

**Author:** ![fabswt](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/fabswt/32/1110_2.png) [@fabswt](https://discourse.slimframework.com/u/fabswt)\
**Post date:** [March 28, 2020, 12:16pm UTC](https://discourse.slimframework.com/t/recommended-way-to-set-get-remove-cookies/3971/3 "2020-03-28T12:16:56Z")

</div>

Aren’t these only for session cookies? Need something to handle cookies that persist after the user closes the browser.

---

<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:** [March 28, 2020, 2:30pm UTC](https://discourse.slimframework.com/t/recommended-way-to-set-get-remove-cookies/3971/4 "2020-03-28T14:30:37Z")

</div>

> [@fabswt](#):
>
> Aren’t these only for session cookies?

Sessions work with cookies.

> Need something to handle cookies that persist after the user closes the browser.

Cookies will be deleted when the browser is closed, _unless_ they have a specific life-time. It’s possible to keep session even after the browser is closed. Use [session\_set\_cookie\_parameters()](https://www.php.net/manual/en/function.session-set-cookie-params.php) to give the session cookie a non-zero lifetime before starting the session, or set [session.cookie\_lifetime()](https://www.php.net/manual/en/session.configuration.php#ini.session.cookie-lifetime) to non-zero.

How to setup the cookie lifetime depends on the specific library and some options like `gc_maxlifetime` and `cookie_lifetime`:

- [Session Config - laminas-session - Laminas Docs](https://docs.laminas.dev/laminas-session/config/#basic-configuration-options)

---

<div class="post-metadata">

**Author:** ![dominik.zogg](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/dominik.zogg/32/235_2.png) [@dominik.zogg](https://discourse.slimframework.com/u/dominik.zogg)\
**Post date:** [April 1, 2020, 6:53pm UTC](https://discourse.slimframework.com/t/recommended-way-to-set-get-remove-cookies/3971/5 "2020-04-01T18:53:11Z")

</div>

I use the following for cookie handling: [https://github.com/dflydev/dflydev-fig-cookies](https://github.com/dflydev/dflydev-fig-cookies)

---

<div class="post-metadata">

**Author:** ![fabswt](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/fabswt/32/1110_2.png) [@fabswt](https://discourse.slimframework.com/u/fabswt)\
**Post date:** [April 1, 2020, 6:58pm UTC](https://discourse.slimframework.com/t/recommended-way-to-set-get-remove-cookies/3971/6 "2020-04-01T18:58:41Z")

</div>

I mentioned it in the original post.

---

<div class="post-metadata">

**Author:** ![dominik.zogg](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/dominik.zogg/32/235_2.png) [@dominik.zogg](https://discourse.slimframework.com/u/dominik.zogg)\
**Post date:** [April 1, 2020, 7:14pm UTC](https://discourse.slimframework.com/t/recommended-way-to-set-get-remove-cookies/3971/7 "2020-04-01T19:14:01Z")

</div>

My fault, sorry.  
I would suggest to override with a expired cookie, which gets deleted by the browser itself, if there is no better solution.
