PHP: How to Fix Double Posting

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.

Comments 8

  1. JL wrote:

    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.

    Posted 17 Jul 2007 at 2:49 am
  2. jasonjemse wrote:

    thank you for yur help :)

    Posted 02 Aug 2007 at 10:02 pm
  3. ememjammer wrote:

    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

    Posted 17 Sep 2007 at 11:18 am
  4. worm remove wrote:

    kill downloader program liquidater worm liquidate windows antispyware removing malware malware kill removing adware removing trojan horse worm scanner untouched by antyspyware

    Posted 15 Jul 2008 at 10:39 am
  5. wrankerserilk wrote:

    I was adopted by my parents at age two. My primary honour of
    incest blonde lesbian ass lick was when I was in preschool and as my coddle drove me
    effectively, she caressd my crotch as she drove. eunquestionably now she would
    contract to bed roxy pornstars like it big with me and touch me there. busty coitus
    started when I was eight and continued for s-tempered more years. It
    also included my sister femdom male bondage who was four years older and also
    adopted. done I was having interaction with her and this lasted
    for ten years, coequal after she was married. My sister and I lull
    get nice teen pussy together nearly four times a year and use to advantage each other bleeding
    much. I am now 41 and she is 45.

    The experiences hot asian lesbians masturbating were unquestionably favourable, both my spoil and sister
    were tender and kind. I ever after received favourable support
    when I on top of the world them. To say the least, I was trained unquestionably properly in
    pleasing a gay hunks in uniform bird, and my ball can vouch for to that.

    I cannot call to mind a consider of any antagonistic results other than it took me 18
    years latin girls with big boobs lesbo sex after I was married to take to task my bride and I ever after felt
    answerable of holding bankrupt with her. We fantisize gay dating service regularly now.

    Posted 17 Jul 2008 at 5:50 pm
  6. Dyeddedagrals wrote:

    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.

    Posted 19 Jul 2008 at 9:47 am
  7. kolikovaa wrote:

    up
    foto and video sex

    Posted 22 Jul 2008 at 2:10 am
  8. XRumerIsTheBest wrote:

    ??????? ??????? ?????, ????????? ????? mkaz.com ;)

    ???????? ???, ??????????, ?? ????????? ????????…
    - ????? ????????? ????? ????????????? ?? ????????? ?????? ?????????????? ????? ?? mail.ru ? ?????? ?????? ???????????
    - ????? ????????? ????? ????????? ????????? ?? mamba.ru ? loveplanet.ru ?? ???????? ??????????, ??? ???? ??? ??????????? ??????? ??????????????
    - ? ????? ?????? ????????? ?? ??????? ????? (????????) “??? ?????? ????????”, ? ????? ? ????? ?? ???? ????? ?? ??????? ????? ? IP ???????? (????????) “?????? ?? ????? megavalenki.ru!”?
    - ???? ?????????? ???????? ? ??????? ?-?? “??? ???????? ?? ???? ?????????”, “??????? ????? 2+2?” ? “????? ?????? ????” ? ????? ????????? ?? ??? ?????????
    - ????? ????????? ?????? ????????? ?????? ?? ???????, ??????? ????????????? ???????????????? ?? ??? ? ???????? ????????? ????? ? ??????????? ???????
    - ? ??? ???? ???????? ? ?????????????? ???????? - phpBB, VBulletin, IPB, ExBB, Icon Board, YaBB, UltimateBB, ?????????? ????????? ????????, ????? ? ???????
    - ????? ????????? ?? ?????? ?????????? ??? ???? ?????
    - ????? ????????? ????????????? ????????? ?????? / SOCKS, ??????????? ??? ?????? ???????????? (?????????? ?????? ?????? ???? ??????)
    - ????? ????????? ????? ????????? ???????????? ????????? ???? ????????????? ??????? phpBB, IPB, VBulletin?
    - ????? ????????? ??????????? ???? ???? ?????? ?? Google PageRank?
    - ????? ????????? ??????? ????????????? ??? ???? ????? ??????????? ?????????? ?? ????????
    - ? ??? ???? ??? ????????? ??????????? ? ????????????????.

    ????? ????: ??? ??? ? ?????? ?????? ??? ???? ???????????? ????????? XRumer 4.085 Platinum Edition + Hrefer 2.85
    ?????? ???????? ????? ????????? ??????? ?? ???????????? ?????????? (Washington Post, Wikipedia ? ?.?.), ????? ??????? ????????? ???????? ????? 3-? ???.

    Just Google! ;)

    ??. ?????: ????? ???, XRumer 3.0 ???????, XRumer 2.9 ???????, white SEO, ????????????? ?????, ????????????? ????????? ??????, ???????? ????????, ?????? 2.9 ???????, ???????, ????????? ??? SEO, ?????? ???, ???, ??????, ?????? 3.0 ???????, XRumer forever, ????????????? ??????????? ??????, ???? ??? SEO, ???????, doorways, ??????? ?? ??????, ???????? ?? ???????, ???? ??? ???, black SEO, ????????, XRumer, XRumer Platinum, ??????, SEO, ????????? ??? ???, ?????? ????????, ????????? ??? SEO, ???

    Posted 27 Jul 2008 at 6:14 pm

Post a Comment

Your email is never published nor shared. Required fields are marked *