mkaz.com

PHP: How to Fix Double Posting

By marcus • Jul 3rd, 2007 • Category: geek

When submitting a form, a user may click the submit button twice, they may reload the posted form, or hit back in their browser. Most of these will cause the form to be submitted twice and likely posted twice to the database.

Here’s one way to solve double posting, the process is:

  • generate a unique key and include it with the form HTML
  • when processing the form insert the unique key into the database table: transactions
    • the transactions table has a UNIQUE INDEX on the key field
    • if the insert fails, means there is a duplicate key, bail out of creating new one
  • batch job runs daily to clean up old transactions so the insert and index is kept tidy

Here’s a bit of code, writing it on-the-fly, so you may have to fix any typos or forgotten semi-colons. We use smarty templates and have custom functions built, so not as easy to share the exact code.

First you need to setup your transactions table:

CREATE TABLE transactions (
    transaction_key varchar(24),
    UNIQUE INDEX uki (transaction_key)
);

A simple PHP function to get a unique id, abstracted in case you want to change it later:

function get_transaction_key() {
    return uniqid(”, true);
}

The main function which checks if the key has been used before:

function check_transaction_key($key) {
    $return_value = db_insert(” INSERT INTO transactions (transaction_key) VALUES (’$key’) “);
    if ($return_value === false) { return false; }
    else { return true; }
}

Your PHP+HTML Template Script:

<form>
    <input type=”hidden” name=”transaction_key” value=”<?=get_transcaction_key()?>”>
    … extra form stuff…
</form>

Your PHP Form Process Script:

if (check_transaction_key($_REQUEST['transaction_key'])) {
    … fun form processing …
}

NOTE: The sample code above should not be considered best practices. Your PHP+HTML should be in a nice template and you should scrub all your submitted variables before doing anything with them.


8 Responses »

  1. Or else, using javascript, you can hide the submit button when it’s been clicked once. It won’t help against spam (you probably already have a blocker anyway), but it’s easier to do.

  2. thank you for yur help :)

  3. So I was off work and surfing and found this place and thought I would join up. I don’t have much more to say right now except I need to start reading some of the older posts to get up to speed before I can start posting.

    Em

  4. http://www.hairstylestop.com/wp-content/uploads/2008/07/eva_longoria.jpg
    Eva Longoria took a break from filming on the set of Desperate Housewives the other day to spend some one on one time with her man, NBA superstar Tony Parker. And Eva didn t just take a break from filming, but also from her hair extensions. Yes it seems this is one of Eva s lesser known beauty secrets, that she works with hair extensions for her character on Desperate Houswives. So here she is seen in all her natural glory as she hangs out with her Tony. Even though her hair is a mite shorter than her locks on Housewives, it looks like she still has some natural wave to it, and wears it in a very simple style when just hanging out. She s given herself a side part, and side swept her bangs for a very sexy, casual look. So for those of you that have always been jealous of Eva s luscious locks, covet no more as she doesn t come by them naturally. She gets by with a little help from her friends.
    Welcome to the Hairstyles Top. Here you will find the latest top hair style pictures, and advice for new hairstyles: wedding hairstyles, short hairstyles, black hairstyles and teen hairstyles, etc. Lots of celebrity haircuts.

  5. Hi!

    I want to improve my SQL knowledge.
    I red that many SQL resources and want to
    read more about SQL for my work as db2 database manager.

    What can you recommend?

    Thanks,
    Werutz

  6. thanks :) youre idea is good!

  7. Hi Everbody

    I just became a member of this forum

    Great job by the admin, mods and seriously every member around.

    A few days ago I read that there is a cure for diabetes on http://www.healthcaredaily.org
    Is this way of curing diabetes mentioned actually true, If so I should have found out earlier! The source looks like a reliable healthcare news website

    Could you someone tell me if this healthcare information is for real?

    Thanks

    SeixSpoiweell

  8. B PLANI devrede Cumhurbaskan iyi Ve Irak’a su sozu