> 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/csrf/overview.md).

# Overview

Csrf gives you a small session-backed token guard for state-changing requests.

Use it when you need to issue a token during page rendering and verify that the same request later posts it back before you mutate application state.

## What the package provides

* `Quantum\Csrf\Csrf` for token generation and validation
* helper access through `csrf()` and `csrf_token()`
* single-use validation that clears the stored token after a successful match

## Quick example

Render a token into a form:

```php
<input type="hidden" name="csrf-token" value="<?= csrf_token(); ?>">
```

Validate it before handling a state-changing request:

```php
csrf()->checkToken(request());
```

## How it works in practice

The package stores one token in the current session under `csrf-token`.

* `csrf_token()` creates the token the first time you call it in the current token lifecycle
* repeated calls return the same stored token until validation succeeds or the session entry is cleared
* `checkToken()` compares the incoming request token with the session token and then deletes the stored token on success

That makes the default flow single-use.

## Important behavior

* the package keeps only one active token per session at a time
* successful validation removes the token from both session storage and the current request object
* missing or mismatched tokens throw `CsrfException`
* `csrf_token()` depends on `APP_KEY`; if that value is missing, the helper throws an app exception instead of returning a token


---

# 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/csrf/overview.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.
