tags:

views:

633

answers:

2

I discovered the Security Component in CakePHP helps to prevent CSRF by adding tokens as hidden values to forms.

What I was wondering is if there was anyway to prevent duplicate form submissions using this Component or some other component/helper?

In previous projects, I used a unique hash saved in a session, which is read then deleted upon submit. A repeated submit would have that same hash and an error would be produced.

thanks

A: 

Don't know about cake, but try to not display content on a POST request, do a self redirect. The double post will get solved.

Mario
Not particularly helpful -- cake is setup generally to have forms post back to themselves. Best to continue this, too, because you get lots of stuff (like automatically populating fields on a validation error) with that.
Travis Leleu
+2  A: 

You could implement the same type of thing in Cake as you've done before.

On submit, set a session variable that marks that form as having been submitted. Make sure to put an expiry time after it (within a few seconds should do the trick). If the session variable is there when you process the form (and you're within that expiration time), then you've got a resubmit, so don't save the form data.

I'd recommend doing this within the save(..) method of your model, so you don't need to worry about adding it in multiple code locations.

Travis Leleu
I see where you are coming from, but don't think you should be accessing SessionComponent from the model layer. :)
deizel
I agree, it's not pretty. Sometimes, however, I'm willing to break the MVC pattern in the interest of getting something done.
Travis Leleu