Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


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,693 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 )