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


Leandro Severino
Leandro Severino
12,674 Points

WHy the need to check the request method: if($_SERVER['REQUEST_METHOD'] == "POST")?

WHy the need to check the request method:

if($_SERVER['REQUEST_METHOD'] == "POST") { //do something }

if you define the method yourself via "method='post'"

Is there some other reason or its just good practice to check every time?

Simon Coates
Simon Coates
28,694 Points

In the scenarios they show that code, the code usually handles both a get/post (ie. the 'sticky form' pattern). You typically only want to do something (affect a permanent change) if using a post request. So in these cases, testing the method takes you down one or two distinct cases. However, I think it is best practice as well to police which method is in use, either for security reasons or just for increased predictability. It is possible for people to modify your forms or write code that bypasses your UI completely. Accessing a script using the wrong method might be an indication that someone is up to something - this may be a fairly weak protection. (see https://security.stackexchange.com/questions/8225/should-i-prevent-sending-of-get-requests-for-urls-that-are-normally-operated-wit )