Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
San Yuk Cheng
Courses Plus Student 1,486 PointsShirts 4 Mike project: Question about header (redirecting a page) ?
So I added the header("location: contact-thanks.php") on my contact-process.php page. The videos explains it would avoid re-sending the email if the client hits the back button. But, I am a little confused. From my understanding, when you fill in the form. It sends you to the contact-process.php which gathers the information in the $_POST array, then sends the email to the client, and redirects you to contact-thanks.php. So, it should be contact.php --> contact-process.php -- > contact-thanks.php.
Then why would it skip contact-process.php when you hit the back button on contact-thanks.php with the header command?
I hope my question should be clear!
Thanks!!
1 Answer
Victor Arellano
12,045 PointsIt first checks for the request method with $_SERVER["REQUEST_METHOD"].
If the method is POST, it means that the form was submitted, and the application will try to send the email. Once the mail is sent, it will redirect you to contact.php?status=thanks. If you hit the back button it will only navigate to the previous page without sending a post request to the server, so the email is not sent again.
Also, there isn't a page named contact-thanks.php or contact-process.php, there is only one page named contact.php that checks for get variables to know what information it should display. Such variables are specified in the URL after the ? sign. If the $status variable is equal to "thanks", it will show a message, otherwise it will display the contact form.
Hope this helps.
Victor Arellano
12,045 PointsVictor Arellano
12,045 PointsI re-read your question. The back button will skip the contact-process.php page because it will take you to the previous cached page, which is contact.php
However, you should still check for the request method in contact-process.php in case it is accesed directly and not on the form submission. If there isn't a post request, redirect to contact.php or another page.