What are some potential ways to track and prevent duplicate form submissions in PHP applications?

One potential way to track and prevent duplicate form submissions in PHP applications is by using a token or unique identifier that is generated and stored upon form submission. This token can be checked against previous submissions to ensure that the form is not submitted more than once.

session_start();

// Generate a unique token
$token = md5(uniqid(rand(), true));

// Store the token in a session variable
$_SESSION['form_token'] = $token;

// Check if the token matches the one stored in the session
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['form_token']) && $_POST['form_token'] == $_SESSION['form_token']) {
    // Process form submission
    // Clear the token to prevent duplicate submissions
    unset($_SESSION['form_token']);
}