EllisLab text mark
Advanced Search
     
$_POST issue
Posted: 08 September 2007 10:22 AM   [ Ignore ]
Joined: 2007-04-06
67 posts

Hi again, guys.

I think, now I have very simple question for advanced programmers. How can I prevent repetitive inserts into DB (with identical data) when a user manually refreshes a page after a valid form submission?? I tried after form submission to unset $_POST variable but no results :/ I’m feeling dummy about this question red face

p.s.: form’s action link points to itself

 Signature 

My Celebrity Blog, My Magento Blog

 
Posted: 08 September 2007 10:53 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-07
65 posts

my strategy is to always do a location redirect in my controller after a form submission

 Signature 

Mac Pro 4x3ghz, 1.8TB HD, 4G RAM, Dual 20” Screens in the house ... *drool*

 
Posted: 08 September 2007 11:23 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2007-04-06
67 posts

I knew that redirect helps in this case, there is no other way ?

 Signature 

My Celebrity Blog, My Magento Blog

 
Posted: 08 September 2007 04:03 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2006-12-20
88 posts
PoWah - 08 September 2007 02:22 PM

Hi again, guys.

I think, now I have very simple question for advanced programmers. How can I prevent repetitive inserts into DB (with identical data) when a user manually refreshes a page after a valid form submission?? I tried after form submission to unset $_POST variable but no results :/ I’m feeling dummy about this question red face

p.s.: form’s action link points to itself

Just an insight $_POST variables are unset by CI by default so unset($_POST) wouldn’t help anything. Also, when a user hits refresh it is in fact resetting the same $_POST data.

Also, redirecting is not a true solution because the user could easily hit “back” a few times and the browser would ask you to resubmit the form data.

Okay, with all that said what you COULD do is assign a sort of “form session”

1) When creating a form set a unique “form session” id (some random string) in the user’s session.
2) Set inside the form params (a hidden field) that same unique “form session” id.
3) Upon submission check to make sure the “form session” id in the form and the session match.
4) If they do match then add the data to your db and then change (or unset) the “form session” id.

Viola!

 
Posted: 08 September 2007 05:59 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2007-08-30
8 posts

read this

 
Posted: 08 September 2007 07:23 PM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2007-07-17
122 posts

you could also make a unique index in your database to prevent duplicate entries. When inserting the data from the form, check if the query was successful and display an error, it won’t be if the index fails

 Signature 

Computers are incredibly fast, accurate and stupid. Human beings are incredibly slow, inaccurate and brilliant. Together they are powerful beyond imagination.

                                    A. Einstein

 
Posted: 08 September 2007 07:30 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2007-04-06
67 posts

Indexes in my case not possible (data may be very similar in various ways), but I think I will use Athfar’s mentioned method. Thanks all for your help!

 Signature 

My Celebrity Blog, My Magento Blog

 
Posted: 08 September 2007 07:36 PM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Joined: 2007-04-06
67 posts

Besides, if how Athfar said, in CI $_POST is unset automatically, then why I can access $_POST variables like this (directly through the $_POST array)?

echo $_POST["some_input"]
 Signature 

My Celebrity Blog, My Magento Blog

 
Posted: 08 September 2007 07:40 PM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2006-12-20
88 posts
PoWah - 08 September 2007 11:36 PM

Besides, if how Athfar said, in CI $_POST is unset automatically, then why I can access $_POST variables like this (directly through the $_POST array)?

echo $_POST["some_input"]

Grr… that’s my mistake it’s $_GET that is unset… $_POST is not filtered. Either way unsetting $_POST in the controller won’t help if you are using $this->input->post() (nor with what you are trying to do).

The security filtering function is called automatically when a new controller is invoked. It does the following:

Destroys the global GET array. Since CodeIgniter does not utilize GET strings, there is no reason to allow it.
Destroys all global variables in the event register_globals is turned on.
Filters the POST/COOKIE array keys, permitting only alpha-numeric (and a few other) characters.
Provides XSS (Cross-site Scripting Hacks) filtering. This can be enabled globally, or upon request.
Standardizes newline characters to \n