> For the complete documentation index, see [llms.txt](https://quantumphp.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://quantumphp.gitbook.io/docs/packages/cookie/usage.md).

# Usage

## Read a cookie

```php
$userId = cookie()->get('user_id');
```

Use `has()` when the cookie is optional:

```php
if (cookie()->has('remember_token')) {
    $token = cookie()->get('remember_token');
}
```

Because presence checks treat empty values as missing, prefer non-empty strings or arrays for stored values.

## Write a cookie

```php
cookie()->set('remember_token', $token, 60 * 60 * 24 * 30, '/', '', true, true);
```

Arguments:

1. `key`
2. `value`
3. lifetime in seconds from now
4. cookie path
5. domain
6. `secure`
7. `httponly`

`time = 0` creates a session cookie instead of a persistent cookie.

## Store structured data

Cookie values are serialized before encryption, so arrays and other serializable values can round-trip through the API:

```php
cookie()->set('preferences', ['theme' => 'dark', 'compact' => true], 3600);

$preferences = cookie()->get('preferences');
```

Keep payloads small. This package does not add cookie size management or chunking.

## Delete one cookie or clear all cookies

```php
cookie()->delete('remember_token');
cookie()->flush();
```

`flush()` deletes every cookie currently visible in the wrapper.

## Integration notes

The package relies on Quantum encryption helpers under the hood. If encryption cannot encode or decode a value, the cookie call fails instead of falling back to plaintext.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://quantumphp.gitbook.io/docs/packages/cookie/usage.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
