Welcome to WebmasterWorld Guest from 107.21.149.142

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP mailing system and use of sessions

     
3:19 pm on Jul 6, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Oct 25, 2006
posts: 1010
votes: 0


When there is mail PHP function and it is validated with redirection to Thank you page, I like to know how to avoid when sending contact forms. When users RELOAD page at Thank you page it will send as many emails (none content) as it is reloaded.

How to avoid this in the correct way with PHP script.
9:42 pm on July 6, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10562
votes: 14


if you are properly redirecting to a thank you page there shouldn't be any way for a refresh of the thank you page to resend the POST request.
5:54 pm on July 7, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 7, 2003
posts:4783
votes: 0


I'll try to elaborate a bit:

The correct way:

You have a form sent to the browser in HTML - PHP script or static or... doesn't matter.

Inside the form there's an action="...." that points to a PHP script on your server.
When the user submits the form, that script will receive a GET or POST request and the script will execute on the server.

The script upon success does what it needs to do and emits a 302 redirect to the browser, pointing to another URL that shows the thank you page (can be another PHP script, static content, ... whatever).

The browser contacts the server for the thank you page.

If the user presses refresh all they do is reload the thank you page; if the user goes back they end up on the form again, which they could resubmit.

If you want to avoid that last, you have a bit of work as you now need to know to keep track of state and have the server know who already submitted what form etc. [Multiple techniques exist. But I doubt it's your problem, nor is it a problem with contact forms in general - yet].
11:26 am on July 8, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Oct 25, 2006
posts: 1010
votes: 0


This is an issue:
If the user presses refresh all they do is reload the thank you page;

thank you page should UNset session variables and mail function should not be executed.
2:46 pm on July 8, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member swa66 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 7, 2003
posts:4783
votes: 0


Nope.

The redirect issued by the script getting the input from the form makes sure the thank you page doesn't do any processing. SO you can leave session etc. all intact.

Just DO NOT output the thank you from the script that processes the form input. A redirect inbetween these is essential.
1:21 pm on July 9, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Oct 25, 2006
posts: 1010
votes: 0


Just DO NOT output the thank you from the script that processes the form input.

I'm new to this. How to DO NOT output the thank you.
1:50 pm on July 9, 2013 (gmt 0)

Full Member

10+ Year Member

joined:May 27, 2006
posts: 227
votes: 0


After you finish sending email from your php script just redirect the page to thankyou.htm page.

Redirection code in php is as below.
header("Location:thankyoupage.htm");

So even if user refreshes the page it will be thankyou.htm and not your php page.

Thanks
Rajiv