Core\SignedCookie::seal
Core\SignedCookie::seal stores a value in a cookie. The browser keeps the cookie, but it cannot read or change the value. seal encrypts the value with a secret key and returns text that you put in a Set-Cookie header. When the browser sends the cookie back, Core\SignedCookie::open checks it and returns the value.
You give seal the name of the cookie, such as cart. open needs the same name. A visitor who sends the value back under another cookie's name gets an error.
You give seal a list of keys, newest first, and the newest key encrypts the value. After you add a new key, keep the old one in the list for a while. Then cookies made with the old key still work.
Each call returns different text, even for the same value. The cookie is about 4/3 × (the length of the value + 40) characters long. A browser keeps about 4096 characters per cookie, so store small values, such as a user id.
The examples below seal and open a value, show that the text changes on each call, and keep a shopping cart in a cookie.
Parameters
$namestringThe name of the cookie, such ascart.openneeds the same name. A cookie sent back under another name does not open.$valuestringThe value to store.openreturns exactly this string.$keysarray<secret bytes>The keys, newest first.$keys[0]encrypts the value. The older keys are there so thatopenstill accepts cookies made before you added a new key. A list of one key is[$key].
Return value
Returns string. The cookie text. It contains only A-Z, a-z, 0-9, - and _, so a Set-Cookie header can carry it without escaping. It is about 4/3 × (length + 40) characters long. Each call returns different text for the same value, because each call adds new random bytes.
Errors
LogicError$keysis empty, or its first key is not 32 bytes long.Core\Crypto::generateKey()returns a key of the right length.RuntimeErrorThe request does not have enough memory left for the cookie.
Examples
Seal and open
<?nvs// Shows how to store a value in a cookie and read it back.
secret bytes $key = Core\Crypto::generateKey();array<secret bytes> $keys = [$key];
// `seal` encrypts the value. The cookie contains only letters, digits, `-` and `_`.string $cookie = Core\SignedCookie::seal('user', 'user=42', $keys);
// The value has 7 characters, so the cookie has 63. This prints 63.echo Core\Str::length($cookie), "\n";
// `open` returns the value again. This prints user=42.echo Core\SignedCookie::open('user', $cookie, $keys), "\n";63user=42New text every time
<?nvs// Shows that sealing the same value twice gives two different cookies.
array<secret bytes> $keys = [Core\Crypto::generateKey()];
// Each call adds new random bytes, so this prints `different`.// Nobody can see from the cookies that two users have the same value.string $first = Core\SignedCookie::seal('theme', 'theme=dark', $keys);string $second = Core\SignedCookie::seal('theme', 'theme=dark', $keys);echo $first == $second ? "the same\n" : "different\n";
// Both cookies still contain the same value. This prints theme=dark two times.echo Core\SignedCookie::open('theme', $first, $keys), "\n";echo Core\SignedCookie::open('theme', $second, $keys), "\n";differenttheme=darktheme=darkA shopping cart cookie
<?nvs// Shows how a shop keeps a shopping cart in a cookie, so the server stores nothing.
// The server reads its key from its configuration. This example makes a new one.array<secret bytes> $keys = [Core\Crypto::generateKey()];
// The first request adds two cups. The cart is written as JSON and sealed.array<int> $cart = ['cup' => 2];string $cookie = Core\SignedCookie::seal('cart', Core\Json::encode($cart), $keys);
// The next request sends the cookie back. The server opens it and adds a teapot.var $stored = Core\Json::decode(Core\SignedCookie::open('cart', $cookie, $keys)) as array<mixed>;array<int> $next = [];foreach ($stored as tainted string $item => mixed $count) { // Only this server can seal the cookie, so the item names are its own. $next[Core\Taint::assertTrusted($item, 'the server sealed this cookie')] = $count as int;}$next['teapot'] = 1;$cookie = Core\SignedCookie::seal('cart', Core\Json::encode($next), $keys);
// The third request reads the cart. This prints {"cup":2,"teapot":1}.echo Core\SignedCookie::open('cart', $cookie, $keys), "\n";{"cup":2,"teapot":1}