Reading the tutorial here: https://github.com/defuse/php-encryption/blob/master/docs/Tutorial.md (scenario #2)

It is advised to store the encryption key in a session variable. I was wondering, is it safer to store the key in a cookie variable instead?

My assumption:

  • $_SESSION is stored in the server
  • $_COOKIE is stored in the client's device (browser)

Threat model:

  • Against employees of hosting provider snooping data
  • Against hackers (they will likely attack the server instead of a user's device)


Notes for clarification in comments for @SteffenUllrich. Below are the exact quotes in the tutorial.

SteffenUllrich: "Where the encrypted data are located..."

Alice wants to host Dave's web application, including its database, on her server

SteffenUllrich: "recommendation regarding storing the key in the session.."

save the Key object somewhere safe (like temporary memory-backed session storage)

  • I think this question needs more information, especially where the encrypted data are located in comparison to the key. They should not be at the same place, i.e. both on the local machine or both on the network. Apart from that I cannot see any recommendation regarding storing the key in the session variable in the tutorial you reference so where does this advice comes from and how is it justified? Please add this information to the question and not in a comment. Sep 11, 2016 at 4:31
  • @SteffenUllrich As mentioned, it follows Scenario #2 in the tutorial. I updated the question for you.
    – IMB
    Sep 11, 2016 at 14:33
  • The advise does not mention $_SESSION but talks about some session storage with the important features of temporary and memory-backed. As far as I know there are different implementations possible for the session object, including cookies or database. While the last one is stored on the server like you assume it is definitely not memory-backed and maybe not even temporary. It is definitely not a good idea to keep the key together with the encrypted data on disk. Sep 11, 2016 at 14:53
  • @SteffenUllrich AFAIK it is possible to set the session handler of $_SESSION to memcache which is a memory based storage. Besides you also can modify the behavior of $_SESSION to clean-up periodically whether or not it is memory-based. Anyway regardless... what is the general consensus in $_SESSION vs $_COOKIES as far as crypto key storage is concerned?
    – IMB
    Sep 11, 2016 at 14:57
  • As usual, Steffen's answer is absolutely correct. However, as is often the case when security professionals start talking, the CI&A takes precedence over what the software is actually intended to do. Both cookie and session provide temporary storage for the key. Unless you want the data access to be exclusive to a specific client device, then you need to think about how an encryption key is created, remains available to the user, populated into the application, optionally recovered....etc. This then has an impact on your design for managing the key over short periods when it is in use.
    – symcbean
    Sep 11, 2016 at 22:09

1 Answer 1


The important thing is that key and encrypted data should not be stored at the same place, i.e. not both on disk, not both at the client side (no sending of encrypted data and storing the key in the cookie) etc. If the encrypted data remain on the server side both a cookie and a server side in-memory session object should be safe enough. Cookies might be even safer than a server side in-memory session object because they are more temporary, i.e. the client will provide the key if needed and if not needed the key will not exist on the server at all, not even in memory.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .