Are there best practices for using cookies in PHP to prevent spam submissions in forms?

To prevent spam submissions in forms, one common practice is to use cookies in PHP to track form submissions. By setting a cookie when a form is submitted and checking for the presence of this cookie before allowing another submission, you can limit the number of submissions from a single user.

// Check if the form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Check if the cookie is set
    if (!isset($_COOKIE['form_submitted'])) {
        // Process the form submission
        // Set a cookie to track the submission
        setcookie('form_submitted', 'true', time() + 3600); // Cookie expires in 1 hour
    } else {
        // Display an error message or redirect the user
        echo 'You have already submitted the form.';
        exit;
    }
}