472,146 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

How to handle user hitting the browser refresh

What are best practices to check to see if a .php script is called for the
first time versus a user hitting a refresh button on their browser?

I am playing around with a PHP front-end to a database. If the user enters
values in a form (form.html), hits submit, the PHP script (insert.php) puts
the submitted fields into the database as intended. insert.php displays a
success message if successful or an error message otherwise. If the user
hits the brower refresh button while at insert.php, the data is re-entered,
and I end up with a duplicate entry in the database.

This is the default behavior of Opera. IE prompts if I want to resend the
data vs. not doing anything. If the data is resent, then the duplicate
entry happens. I need to guard against this either way.

Thanks for your help.

Jul 17 '05 #1
6 2795
Mark Feller wrote:
What are best practices to check to see if a .php script is called for the
first time versus a user hitting a refresh button on their browser?

I am playing around with a PHP front-end to a database. If the user enters
values in a form (form.html), hits submit, the PHP script (insert.php) puts
the submitted fields into the database as intended. insert.php displays a
success message if successful or an error message otherwise. If the user
hits the brower refresh button while at insert.php, the data is re-entered,
and I end up with a duplicate entry in the database.

This is the default behavior of Opera. IE prompts if I want to resend the
data vs. not doing anything. If the data is resent, then the duplicate
entry happens. I need to guard against this either way.

Thanks for your help.


Ooh ooh, I know this one.

Use the header function at the end of a successful insert to send the
user to a different page (e.g., header("location:
insert_complete.php");). If they reload that, all it does is re-display
the insert complete page - no duplicate entry.

I handle errors in the same file (insert.php) with a form based back
button to the form.html.

--
*****************************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*****************************
Jul 17 '05 #2
Mark Feller wrote:
What are best practices to check to see if a .php script is called for the
first time versus a user hitting a refresh button on their browser?

I am playing around with a PHP front-end to a database. If the user
enters values in a form (form.html), hits submit, the PHP script
(insert.php) puts
the submitted fields into the database as intended. insert.php displays a
success message if successful or an error message otherwise. If the user
hits the brower refresh button while at insert.php, the data is
re-entered, and I end up with a duplicate entry in the database.

This is the default behavior of Opera. IE prompts if I want to resend the
data vs. not doing anything. If the data is resent, then the duplicate
entry happens. I need to guard against this either way.

Thanks for your help.

as Chuck mentioned redirect the user after successful submission to the
database. ( perhaps the db action should not be in a page delivered to the
user, but a separate script, you can set the script to run in the action
attribute of the form element )
G
--
http://www.kolumbus.fi/bob.smith
Jul 17 '05 #3
SOR
<comp.lang.php , Mark Feller , mj***@lycos.com>
<4L****************@newssvr33.news.prodigy.com>
<Fri, 15 Jul 2005 20:10:40 GMT>
If the user
hits the brower refresh button while at insert.php, the data is re-entered,
and I end up with a duplicate entry in the database.

This is the default behavior of Opera. IE prompts if I want to resend the
data vs. not doing anything. If the data is resent, then the duplicate
entry happens. I need to guard against this either way.


I need to do the same thing .

Was thinking of using the users ip address and a flatfile .

- visit webpage
- get the users ip address
- get stored flatfile ip address
- check and compare this ip address to the stored flatfile ip address

- if same - exit
- if different - save ip address to flatfile - proceed

Havent tried it yet though - although in theory it shouldnt have any
problems .

Jul 17 '05 #4
SOR
<comp.lang.php , SOR , we*******@sparesorrepair.co.uk.INVALID>
<MP************************@no-cancel.newsreader.com>
<Sat, 16 Jul 2005 18:41:12 +0100>
Was thinking of using the users ip address and a flatfile .

- visit webpage
- get the users ip address
- get stored flatfile ip address
- check and compare this ip address to the stored flatfile ip address

- if same - exit
- if different - save ip address to flatfile - proceed

Havent tried it yet though


Seems to work fine on www.yellowvultures.co.uk having just tried it .
Jul 17 '05 #5
"SOR" <we*******@sparesorrepair.co.uk.INVALID> wrote in message
news:MP************************@no-cancel.newsreader.com...

Was thinking of using the users ip address and a flatfile .

- visit webpage
- get the users ip address
- get stored flatfile ip address
- check and compare this ip address to the stored flatfile ip address

- if same - exit
- if different - save ip address to flatfile - proceed

Havent tried it yet though


Seems to work fine on www.yellowvultures.co.uk having just tried it .


Be aware that visitors from AOL (just to cite one example) continually
change their IP address (just hitting refresh will often change the IP).
And there are many other mechanisms on the web that break the relationship
between a person and their IP, as seen by your server. Search this group
for many many discussions on this point.
Jul 17 '05 #6
SOR
<comp.lang.php , Dana Cartwright , da******@weavemaker.com>
<B8******************@twister.nyroc.rr.com>
<Sat, 16 Jul 2005 19:23:13 GMT>
- visit webpage
- get the users ip address
- get stored flatfile ip address
- check and compare this ip address to the stored flatfile ip address

- if same - exit
- if different - save ip address to flatfile - proceed

Havent tried it yet though


Seems to work fine on www.yellowvultures.co.uk having just tried it .


Be aware that visitors from AOL (just to cite one example) continually
change their IP address (just hitting refresh will often change the IP).
And there are many other mechanisms on the web that break the relationship
between a person and their IP, as seen by your server. Search this group
for many many discussions on this point.


Fair enough - but better than nothing I suppose .
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by milkyway | last post: by
1 post views Thread by Chirag Malvi | last post: by
3 posts views Thread by George Durzi | last post: by
2 posts views Thread by Jaikanth Sithambaranathan via .NET 247 | last post: by
15 posts views Thread by tmax | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.