Content · Web Development · PHP

Avoiding Duplicate Form Submissions

Use one-time form tokens for accidental resubmits and durable transaction-level idempotency for important operations.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Use one-time form tokens for accidental resubmits and durable transaction-level idempotency for important operations. The examples below demonstrate the main workflow, an edge case and one practical integration pattern.

What Does This Workflow Do?


Use the first example to see the core idea. The additional examples demonstrate validation and a separate integration scenario. Validate assumptions at trust boundaries and handle failure paths explicitly rather than relying only on a successful example.

Syntax:

The complete workflow is demonstrated below; individual calls are explained in context.

Requirements and Data Flow


There is no single function signature for this workflow. The required variables depend on the request, server configuration and libraries used in the examples.

Example 1: Basic Usage


Use one-time form tokens for accidental resubmits and durable transaction-level idempotency for important operations.

<?php
session_start();
$_SESSION["form_token"] ??= bin2hex(random_bytes(16));
if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $submitted = (string) ($_POST["form_token"] ?? "");
    if (!hash_equals($_SESSION["form_token"], $submitted)) { http_response_code(409); exit("Already submitted or invalid"); }
    unset($_SESSION["form_token"]);
}

This example demonstrates the central operation. The final output depends on the supplied input and, where relevant, the runtime or connected service.

Example 2: Handling a Different Case


<?php
$firstToken = bin2hex(random_bytes(16));
$submittedToken = "invalid-token";
var_dump(hash_equals($firstToken, $submittedToken));

The second example focuses on a different input or failure condition, so the behavior can be distinguished from the basic case.

Practical Example


<?php
$uniqueKey = bin2hex(random_bytes(16));
// Store this key with a UNIQUE constraint in the orders table.
// A repeat submission must return the saved outcome rather than create another order.
echo $uniqueKey;

The practical example demonstrates how the same subject fits into a larger application. Use your own file paths, endpoints and validated inputs rather than copying sample settings directly.

Important Behavior and Edge Cases


  • Validate assumptions at trust boundaries and handle failure paths explicitly rather than relying only on a successful example.
  • Keep the example scoped to its stated purpose. A sample does not replace application authentication, authorization, production configuration or deployment review.
  • Use the documented return type and handle false, null and exceptions according to the specific call.

Comparison With Related Functions


ApproachDifference
Avoiding Duplicate Form SubmissionsUse one-time form tokens for accidental resubmits and durable transaction-level idempotency for important operations.
PHP built-in functionsSolve individual low-level operations rather than complete workflows.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring errors from Avoiding Duplicate Form SubmissionsCheck the actual result and handle the expected error path.
Using the demonstration unchanged in productionAdapt file paths, configuration, authentication and permission rules to the application.
Treating input or returned data as trustedApply validation at input boundaries and context-aware escaping at output boundaries.

Key Takeaways


  • Validate assumptions at trust boundaries and handle failure paths explicitly rather than relying only on a successful example.
  • Keep the example scoped to its stated purpose. A sample does not replace application authentication, authorization, production configuration or deployment review.
  • Use the documented return type and handle false, null and exceptions according to the specific call.

Use one-time form tokens for accidental resubmits and durable transaction-level idempotency for important operations. Use this approach when its capabilities and constraints match the application. Handle invalid data, failure conditions and resource cleanup explicitly.

Share this resourceSend this page to someone who may find it useful.