Cookies Management

Cookies are a very useful way to store small pieces of data on the client’s machine that can be retrieved even if the user closes his/her browser. Phalcon\Http\Response\Cookies acts as a global bag for cookies. Cookies are stored in this bag during the request execution and are sent automatically at the end of the request.

Basic Usage

You can set/get cookies by just accessing the cookies service in any part of the application where services can beaccessed:

  1. <?php
  2. use Phalcon\Mvc\Controller;
  3. class SessionController extends Controller
  4. {
  5. public function loginAction()
  6. {
  7. // Check if the cookie has previously set
  8. if ($this->cookies->has('remember-me')) {
  9. // Get the cookie
  10. $rememberMeCookie = $this->cookies->get('remember-me');
  11. // Get the cookie's value
  12. $value = $rememberMeCookie->getValue();
  13. }
  14. }
  15. public function startAction()
  16. {
  17. $this->cookies->set(
  18. 'remember-me',
  19. 'some value',
  20. time() + 15 * 86400
  21. );
  22. $this->cookies->send();
  23. }
  24. public function logoutAction()
  25. {
  26. $rememberMeCookie = $this->cookies->get('remember-me');
  27. // Delete the cookie
  28. $rememberMeCookie->delete();
  29. }
  30. }

Encryption/Decryption of Cookies

By default, cookies are automatically encrypted before being sent to the client and are decrypted when retrieved from the user. This protection prevents unauthorized users to see the cookies’ contents in the client (browser). Despite this protection, sensitive data should not be stored in cookies.

You can disable encryption as follows:

  1. <?php
  2. use Phalcon\Http\Response\Cookies;
  3. $di->set(
  4. 'cookies',
  5. function () {
  6. $cookies = new Cookies();
  7. $cookies->useEncryption(false);
  8. return $cookies;
  9. }
  10. );

If you wish to use encryption, a global key must be set in the crypt service:

  1. <?php
  2. use Phalcon\Crypt;
  3. $di->set(
  4. 'crypt',
  5. function () {
  6. $crypt = new Crypt();
  7. /**
  8. * Set the cipher algorithm.
  9. *
  10. * The `aes-256-gcm' is the preferable cipher, but it is not usable until the
  11. * openssl library is upgraded, which is available in PHP 7.1.
  12. *
  13. * The `aes-256-ctr' is arguably the best choice for cipher
  14. * algorithm in these days.
  15. */
  16. $crypt->setCipher('aes-256-ctr');
  17. /**
  18. * Setting the encryption key.
  19. *
  20. * The key should have been previously generated in a cryptographically safe way.
  21. *
  22. * Bad key:
  23. * "le password"
  24. *
  25. * Better (but still unsafe):
  26. * "#1dj8$=dp?.ak//j1V$~%*0X"
  27. *
  28. * Good key:
  29. * "T4\xb1\x8d\xa9\x98\x054t7w!z%C*F-Jk\x98\x05\x5c"
  30. *
  31. * Use your own key. Do not copy and paste this example key.
  32. */
  33. $key = "T4\xb1\x8d\xa9\x98\x054t7w!z%C*F-Jk\x98\x05\x5c";
  34. $crypt->setKey($key);
  35. return $crypt;
  36. }
  37. );

Sending cookies data without encryption to clients including complex objects structures, resultsets, service information, etc. could expose internal application details that could be used by an attacker to attack the application. If you do not want to use encryption, we highly recommend you only send very basic cookie data like numbers or small string literals.