$_POST vs. $_SERVER['REQUEST_METHOD'] == 'POST'
Well, they don't do the same thing, really.
$_SERVER['REQUEST_METHOD']
contains the request method (surprise).
$_POST
contains any post data.
It's possible for a POST request to contain no POST data.
I check the request method — I actually never thought about testing the $_POST
array. I check the required post fields, though. So an empty post request would give the user a lot of error messages - which makes sense to me.
if ($_SERVER['REQUEST_METHOD'] == 'POST')
is the correct way, you can send a post request without any post data.
I used to check $_POST
until I got into a trouble with larger POST data and uploaded files. There are configuration directives post_max_size
and upload_max_filesize
- if any of them is exceeded, $_POST
array is not populated.
So the "safe way" is to check $_SERVER['REQUEST_METHOD']
. You still have to use isset()
on every $_POST
variable though, and it does not matter, whether you check or don't check $_SERVER['REQUEST_METHOD']
.