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

PHP Build a Simple PHP Application Adding a Contact Form Checking the Request Method

header();

Hello,

Can you please explain how

header("Location: contact-thanks.php");

help prevent the browser or server from processing a contac.php form twice and sending a mail twice.

thanks

That line redirects processing from the current position to the file specified - contact-thanks.php

The IF statement in contact.php is the mechanism by which the script decides is the form is to be displayed or the incoming data processed:

First there is a GET request to contact.php, triggered by a normal link. The IF statement evaluates to false, so the processing code is skipped The form is displayed and that request is complete

User presses the submit button, triggering a POST request to contact.php
The IF statement is true, so the processing code is executed and email is sent
The header() statement is processed, redirecting execution to contact-thanks.php. The contact-thanks.php page is processed/displayed and the request is complete

Does that answer your question?

Hello tim221,

Thank you for the detailed response, but I guess what I am still unsure about is when to use header()? I mean why not use an include()? Based on the video lecture, I know that we don't want our contact-process to send another email, so we use header() to redirect to contact-thanks.php.

Well, my 2 questions are: 1) contact-process.php does not have a form with a submit button, so how can it send another email when use the back button?

2) Sorry for asking the same question again, but in contact-process.php why can't we use include('contact-thanks) instead of header('contact-thank.php');

Cheers!

header("Location: xyz.php") means stop right here, change the browser URL to this new value, load the new file and execute it. Nothing else in the current file will be executed.

include 'xyz.php' means insert the contents of xyz.php right here and execute it, then continue with the next statement in the current file.

In the video referred to, contact.php is loaded and one of two things happens:

  1. If there was no form submitted (GET request) then the IF statement returns false and it's code is not executed, so form is displayed.

  2. If there was a form submitted (POST request) then the IF statement returns true. It's code is executed resulting in an email being sent. Then the header() statement is executed, which means the browser URL changes to contact-thanks.php and that page is then displayed. The rest of contact.php is not executed.

If you used include then the contact-thanks.php page would be executed and then the rest of contact.php would also be executed making a bit of a mess!

I recommend experimenting with these commands on your own. Make a new file called 'test.php' which just echoes something. Then include it in one of the existing pages, and then also redirect to it so you can see how it's different.

5 Answers

I totally got this post.

Cool! I don't know how the best answer thing works, but thanks!

It would makes sense to me that everything on contact-process.php was being "displayed" and that once we leave contact-process.php and come back to it again, it makes sense to say everything on that page is being "displayed again" on the browser

That's exactly correct. In the context of PHP, 'displayed' is the same as 'processed' or 'executed'. If the code includes sending an email then that would happen each time too.

but processed to the point that it can send another email??????

It can't send an email containing user data if the form wasn't submitted, but the code can still run and it can try to send an email. Without user data there might be warnings, errors, a blank email, or some combination of those things, but in the case of contact-process.php the code will still run. This is of course a problem, but it's solved solved later in the chapter by putting all the form stuff into a single file.

Any clearer?

Hello tim221,

Thank you so much. I totally get the if else flow and head() and include(), but where is the button to vote for best answer. I will send staff a mail to see if they can help me place a vote for this post.

As for the other post on pressing a back button that will cause contact-process.php to send an email, uh I don't know.... There is really nothing about that page that needs to be processsed. All I see are variables storing values, a html page at the bottom, and some include(), so I am having a hard time imagining anything getting processed on that page. It would makes sense to me that everything on contact-process.php was being "displayed" and that once we leave contact-process.php and come back to it again, it makes sense to say everything on that page is being "displayed again" on the browser, but processed to the point that it can send another email??????

Honestly, thank you so much for all your help. I totally got this post.

Cheers!!!!!!

Hello tim221,

Ok ok, so I should remember that in Php 'process' and 'display' are the same.

And that as long has I have a php file with code that can send an email, I should make sure to divide that file into 2.

The 1st file should contain only the variables and values and header(),or the code to process the mail, and the 2nd file should be the redirected file so that when the user presses the back button, they can only go back and forth between contact.php and contact-thank.php, and never see contact-process.php?

Is that a good way to look at this process?

** I have informed staff about the voting problem on this post

Cheers!!!

Ok ok, so I should remember that in Php 'process' and 'display' are the same.

Yes that's a good way to think about it at this stage.

And that as long has I have a php file with code that can send an email, I should make sure to divide that file into 2.

That by itself doesn't solve the problem because whether it's one or two files, if the form processing commands can be triggered by a GET request then you will potentially have the problems we have been discussing.

The best idea is to only process the form on a POST request, because POST requests can't be accessed via the back button or directly by typing a URL. This is implemented in the following videos in this course, the final result is a typical single-file approach which works well, I recommend you check it out.

I have informed staff about the voting problem on this post

Thanks!

Hello Tim,

I followed with TreeHouse and the votes for this post, and this is what they said:

""Thanks for writing to us. Unfortunately, you cannot vote on answers that are a direct response to a post (see how it's indented to the right, this indicates it was a direct response instead of a new comment within your post) and it's something we're working on fixing for a future update on the forum.

We apologize for the inconvenience.

Best, Rob""

Anway, thanks a lot for your help on this post

Thanks for letting me know and all the best.