Connecting Tech Pros Worldwide Forums | Help | Site Map

How to handle user hitting the browser refresh

Mark Feller
Guest
 
Posts: n/a
#1: Jul 17 '05
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.




Chuck Anderson
Guest
 
Posts: n/a
#2: Jul 17 '05

re: How to handle user hitting the browser refresh


Mark Feller wrote:
[color=blue]
>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.
>
>
>
>
>[/color]
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.
*****************************
Bob Smith
Guest
 
Posts: n/a
#3: Jul 17 '05

re: How to handle user hitting the browser refresh


Mark Feller wrote:
[color=blue]
> 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.[/color]
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
SOR
Guest
 
Posts: n/a
#4: Jul 17 '05

re: How to handle user hitting the browser refresh


<comp.lang.php , Mark Feller , mjf77@lycos.com>
<4LUBe.3730$Ih7.773@newssvr33.news.prodigy.com>
<Fri, 15 Jul 2005 20:10:40 GMT>
[color=blue]
> 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.
>[/color]

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 .

SOR
Guest
 
Posts: n/a
#5: Jul 17 '05

re: How to handle user hitting the browser refresh


<comp.lang.php , SOR , webmaster@sparesorrepair.co.uk.INVALID>
<MPG.1d4355d399c7c1a7989d16@no-cancel.newsreader.com>
<Sat, 16 Jul 2005 18:41:12 +0100>
[color=blue]
> 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
>[/color]

Seems to work fine on www.yellowvultures.co.uk having just tried it .
Dana Cartwright
Guest
 
Posts: n/a
#6: Jul 17 '05

re: How to handle user hitting the browser refresh


"SOR" <webmaster@sparesorrepair.co.uk.INVALID> wrote in message
news:MPG.1d435efb9c38f2f7989d17@no-cancel.newsreader.com...

Was thinking of using the users ip address and a flatfile .[color=blue][color=green]
>>
>> - 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
>>[/color]
>
> Seems to work fine on www.yellowvultures.co.uk having just tried it .[/color]

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.


SOR
Guest
 
Posts: n/a
#7: Jul 17 '05

re: How to handle user hitting the browser refresh


<comp.lang.php , Dana Cartwright , danapub2@weavemaker.com>
<B8dCe.45180$0i3.1483@twister.nyroc.rr.com>
<Sat, 16 Jul 2005 19:23:13 GMT>
[color=blue][color=green][color=darkred]
> >> - 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
> >>[/color]
> >
> > Seems to work fine on www.yellowvultures.co.uk having just tried it .[/color]
>
> 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.
>[/color]

Fair enough - but better than nothing I suppose .
Closed Thread


Similar PHP bytes