Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP ensure data is not entered from page refresh
Old 07-31-2008, 02:57 PM PHP ensure data is not entered from page refresh
Junior Talker

Posts: 2
Trades: 0
Is there any good way to ensure data is not entered into a mysql database table using a page refresh? I am building an application for a photographer that will be ran over an intranet that tracks sales, orders, payments and tasks to complete. I am at the invoice section. I want to ensure that if someone presses the refresh button data isn't inserted twice. The only problem I am having is a record could essentially contain all of the same data, and it wouldn't really be that rare. Ussually I would check a mysql record and ensure it wasn't a duplicate. In this case that will not work.

Btw it is nice to see an active forum, where people are actually here. All of the other web design forums I have stopped by seem to be really dead!
jeverd01 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-31-2008, 05:42 PM Re: PHP ensure data is not entered from page refresh
Super Talker

Posts: 116
Trades: 0
one thing you could do is input an ID number urself as a primary key rather than letting mysql generate it incrementing-ly. when you make the form where the user adds the info, you could find the highest number ID, add one to it, and put it as a hidden field. once they hit submit, all the info will be added plus that ID number. however, since it already had retrieved the highest id number when the form was generated, hitting refresh would send the same id number and you coul djust do a check to see if that id number already exists.

at least, i think that would work... give it a try.
__________________


Please login or register to view this content. Registration is FREE

nasaboy007 is offline
Reply With Quote
View Public Profile Visit nasaboy007's homepage!
 
Old 07-31-2008, 07:33 PM Re: PHP ensure data is not entered from page refresh
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
Originally Posted by nasaboy007 View Post
one thing you could do is input an ID number urself as a primary key rather than letting mysql generate it incrementing-ly. when you make the form where the user adds the info, you could find the highest number ID, add one to it, and put it as a hidden field. once they hit submit, all the info will be added plus that ID number. however, since it already had retrieved the highest id number when the form was generated, hitting refresh would send the same id number and you coul djust do a check to see if that id number already exists.

at least, i think that would work... give it a try.
Excellent answer, but I cannot give you tp anymore. I have to spread some more before ;-)

Just one remark: don't use this key as a field in db.
Check the form hidden field against a session saved value.

like this:
1) user Tom access a page with a form. The PHP script add tho the form an hidden field which contains a random hashed value ( look at http://www.php.net/manual/en/function.uniqid.php ).
At the same time, save that hashed value into the session, like $_SESSION['formHash']
2) On the form processing, start by checking that there is an hidden hash value, and compare it to the session one.
If the values are identical, process the form. If not, redirect the user to an message page, telling him what happened.

One work less solution would be to integrate the generation/session saving of the hash value in a prepended php page. That way, another value would be generated on each page without you to have to think about it.
Just one warning: if you have ajax calls, this will modify the session hash behind the curtain, thus invalidating your currently displayed form.
Think to filter those ajax requests out.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 07-31-2008, 08:00 PM Re: PHP ensure data is not entered from page refresh
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
If your inserting data from a web form from the previous page, you can just have a HTTPD header redirect to the same page after processing to destroy all the post data. If you have no post data, then you should not have any reason to write to the db and actions like refresh and back/forward will not resend post data.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 08-02-2008, 05:31 PM Re: PHP ensure data is not entered from page refresh
Skilled Talker

Posts: 85
Name: Ilyes Rhouma
Trades: 0
What Mgraphic said is great
You can create a httpd redirect to redirect user.
You can do the following:
Page1: the form page, and this form send to Page2.
Page2: the form processing, then a header() function to Page3.
Page3: the final page,
So the user will be in page3, he can refresh with no problem
This is the easiest way I think, just a new file between your two files.
Ilyes is offline
Reply With Quote
View Public Profile Visit Ilyes's homepage!
 
Old 08-02-2008, 08:10 PM Re: PHP ensure data is not entered from page refresh
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Yes, redirect is indeed a good option.
The advantage of the challenge in the form, is to prevent xss injection too.
You ensuer that the form you receive is coming from the one host who was designed to.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 08-03-2008, 01:51 PM Re: PHP ensure data is not entered from page refresh
Junior Talker

Posts: 2
Trades: 0
mgraphic I think I'll go with your idea, seems simple enough. I really don't have to worry about any security issues since it will only be a few people on the application and it will be ran locally rather than via the web.
jeverd01 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP ensure data is not entered from page refresh
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.53118 seconds with 12 queries