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

Rifqi Fahmi
Rifqi Fahmi
23,164 Points

$_REQUEST and $_POST ???

What is the difference between this superglobals $_REQUEST and $_POST ??? I found it in http://www.w3schools.com/php/php_superglobals.asp there are no difference between this two variables, its equally collecting data from the input. Thanks :))

2 Answers

Kevin Korte
Kevin Korte
28,149 Points

The difference is $_REQUEST and $_POST is that $_REQUEST gets $_POST, $_GET, and $_COOKIE.

This sometimes can be useful, but it can also be dangerous, because if you weren't expecting to get a GET or COOKIE param, and you use REQUEST, you're getting it.

For security reasons, it's best to limit form inputs to exactly what you expect, and only what you expect to pass through the form.

So general practice you'll find people use POST, GET, and COOKIE when that's what they expect. I would limit using REQUEST to when you have a reason to use it, like lets say your form has a GET param that ref url in it, but the rest of the form posts via POST, than REQUEST makes sense so you can receive all the params. Just be prepared to handle any extra params that might come in, maliciously.

Which by the way, even if you use POST, GET, or COOKIE, someone still can send in malicious parmas, so you always have to sanitize, every single time.

I'm pretty sure $_POST will only work on http POST request, where as $_REQUEST will work on all http requests

http://stackoverflow.com/questions/1924939/request-vs-get-and-post