22

This should be a elementary question but why is better to use something like this:

$pwd = filter_input(INPUT_POST, 'pwd');

Instead of just:

$pwd = $_POST['pwd'];

PS: I understand that the filter extension can be used with more arguments to provide an additional level of sanitization.

0

3 Answers 3

15

It's not. $_GET, $_POST, $_COOKIE and $_REQUEST are filtered with default filter. filter_input(INPUT_POST, 'pwd') without additional parameters also uses the default filter. So there is no difference at all.

Sign up to request clarification or add additional context in comments.

3 Comments

Was looking around for filter_input vs. htmlspecialchars and noticed this appears to be outdated. Most PHP configurations do not filter this data anymore, the link you post even has the default configuration value set as "unsafe_raw"
@Charles: in both cases data passes through the default filter. That default now happens to be "unsafe_raw", but that doesn't change the fact that using filter_input without filter parameter does not give you any additional level of security.
On my Server there is a difference, when magic_quotes_gpc is turned on. The filtered one does not includeslashe, while referencing it with $_POST has. So far I copuldn't figure out why,... stackoverflow.com/questions/9533122/…
2

Any data which is sent from the client (such as POST data) should be sanitized and escaped (and even better, sanity-checked) to ensure that it isn't going to kill your website.

SQL Injection and Cross-site scripting are the two largest threats for failing to sanitize your user-sent data.

3 Comments

Is filter_input() still necessary if you're using parameterized queries and htmlspecialchars() before you print any user-supplied data?
filter_input(INPUT_POST, 'pwd'); (without any other argument still sanitizes the value?
@Ben: generally you're right, but that's not an answer to this question.
2

It is not better.

Please see docs on filter_input http://www.php.net//manual/en/function.filter-input.php

and click the "Types of Filters" link. http://www.php.net/manual/en/filter.filters.php

I have only ever used the integer filter ...

$user_id = filter_input(INPUT_POST, 'user_id', FILTER_SANITIZE_NUMBER_INT);
$user = abs($user_id); // To get rid of any +/-

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.