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

# Usage

## Add a token to an HTML form

```php
<form method="POST" action="/profile">
    <input type="hidden" name="csrf-token" value="<?= csrf_token(); ?>">

    <!-- other fields -->
</form>
```

This is the most direct package flow: render a token, then expect the same token back on submit.

## Validate before changing state

```php
use Quantum\Csrf\Exceptions\CsrfException;

try {
    csrf()->checkToken(request());

    // continue with the update
} catch (CsrfException $e) {
    // reject the request
}
```

Call validation before writes such as create, update, delete, or other sensitive POST-style actions.

## Regenerate after a successful submission

Because validation is single-use, render or fetch a new token after a successful check if the user will submit another protected request.

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

$newToken = csrf_token();
```

## Know the boundaries

Keep these constraints in mind:

* the package stores only one active token per session
* repeated token generation returns the existing token until validation clears it
* validation throws exceptions instead of returning `false`
* `csrf_token()` cannot work until `APP_KEY` is available

## Good fit for this package

Use Csrf when you want a simple framework-level guard around forms and other state-changing requests.

If you need multiple concurrent tokens per user flow, token scoping per form, or custom rotation rules, you will need to build those rules around the package's single-token contract.


---

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