Cookies

Get Cookies

A Slim application will automatically parse cookies sent with the current HTTP request. You can fetch cookie valueswith the Slim application’s getCookie() helper method like this:

  1. <?php
  2. $app = new \Slim\Slim();
  3. $foo = $app->getCookie('foo');

Only Cookies sent with the current HTTP request are accessible with this method. If you set a cookie during thecurrent request, it will not be accessible with this method until the subsequent request. Bear in mind the \Slim\Slimobject’s getCookie() method is a convenience. You may also retrieve the complete set of HTTP request cookiesdirectly from the \Slim\Http\Request object like this:

  1. <?php
  2. $cookies = $app->request->cookies;

This will return an instance of \Slim\Helper\Set so you can use its simple, standardized interface to inspect therequest’s cookies.

You can optionally choose to encrypt all cookies stored on the HTTP client with the Slim app’s cookies.encryptsetting. When this setting is true, all cookies will be encrypted using your designated secret key and cipher.

It’s really that easy. Cookies will be encrypted automatically before they are sent to the client. They will alsobe decrypted on-demand when you request them with \Slim\Slim::getCookie() during subsequent requests.