I'm learning PHP and specifically how to secure php forms.
I'm reading an article entitled "Sanitize and Validate Data with PHP Filters" wherein the author checks if the form was submitted using the following code:
if (isset($_POST['Submit'])) { // do something... } Which does work, but I've read that its best to use input filters (i.e. filter_input).
Secondly, using filter_input would also stop netbeans from nagging me about not "accessing the superglobal $_POST Array directly"
So I wrote the following:
function is_form_submit() { $request = filter_input(INPUT_SERVER, "REQUEST_METHOD"); return $request === 'POST' ? true : false; } Which could be used like so:
if ( is_form_submit() ) { // do something... } So my question is: doesn't my code achieve the same thing? If not, why not. Please advise.
isset($_POST['Submit'])only works if you have an input element with the name"Submit"whereas$_SERVER['REQUEST_METHOD'] === 'POST'is a more generic solution. To check specifically for a form submission, you could check whether the content type isapplication/x-www-form-urlencodedormultipart/form-datareturn $request === 'POST' ? true : false;===return $request === 'POST';if (!empty($_POST)),normally i used to check the submitted valueissetwith the default parameters that are always sent plus I have tokens sent with each post and validating them tells me that the post was submitted and came from my form.filter_input()requires PHP 5.2+ and it is probably slower thanisset()(not tested)