Currently I'm making sort of calculator-like app in PHP with form as method of input. To secure input i'm using filter_input()
function. As filter this function take one of elements from two groups: FILTER_SANITIZE
and FILTER_VALIDATE
, which one should i use to filter input from form?
$number1 = trim(filter_input(INPUT_GET, 'number1', FILTER_VALIDATE_FLOAT));
or
$number1 = trim(filter_input(INPUT_GET, 'number1', FILTER_SANITIZE_FLOAT));
Validation checks if the input meets a set of criteria (such as a string contains no standalone single quotation marks). Sanitization modifies the input to ensure that it is valid (such as doubling single quotes). You would normally combine these two techniques to provide in-depth defense to your application.
The filter_var() function both validate and sanitize data. The filter_var() function filters a single variable with a specified filter. It takes two pieces of data: The variable you want to check.
Sanitizing data means removing any illegal character from the data. Sanitizing user input is one of the most common tasks in a web application. To make this task easier PHP provides native filter extension that you can use to sanitize the data such as e-mail addresses, URLs, IP addresses, etc.
It depends on what you need or is suitable for your application, really. One would validate it, and say "Yes, this is (or isn't) a valid float", while the other would clean it for any non-acceptable value and return that, and not say anything if the original input was valid or not to begin with.
The same applies for the other FILTER_SANITIZE_*
and FILTER_VALIDATE_*
constants, but in this example we'll look at floating-point validation and sanitation, as asked in the original question.
Let's take a look!
$float = 0.032;
$not_float = "0.03b2";
var_dump(filter_var($float, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION));
var_dump(filter_var($not_float, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION));
var_dump(filter_var($float, FILTER_VALIDATE_FLOAT));
var_dump(filter_var($not_float, FILTER_VALIDATE_FLOAT));
The return from the above dumps would be
string(5) "0.032" // $float FILTER_SANITIZE_NUMBER_FLOAT
string(5) "0.032" // $not_float FILTER_SANITIZE_NUMBER_FLOAT
float(0.032) // $float FILTER_VALIDATE_FLOAT
bool(false) // $not_float FILTER_VALIDATE_FLOAT
FILTER_SANITIZE_NUMBER_FLOAT
would return a string of the sanitized value (PHP isn't a strongly typed language, so "0.032" == 0.032
).
You should also note the FILTER_FLAG_ALLOW_FRACTION
flag, which keeps the decimal in place (without that flag it would return 0032
).
As you can see, any FILTER_VALIDATE_FLOAT
would return a boolean false
if it isn't a valid float, and the actual floating value if it was valid (which is a "truthy" value). Keep in mind that 0.00
would be a "falsy" value, so if you wish to check if the validation failed, you should use strict comparison, in case the input was zero, but still valid.
if (filter_var($input, FILTER_VALIDATE_FLOAT) === false) {
// Oh noes! $input wasn't a valid float!
}
You can see it for yourself in this live demo.
To conclude
If you want to use it in calculations, you might want to validate it, and let the user know that its invalid format, but you could sanitize it, and use it anyway.
Other filters
The examle here shows the usage of FILTER_SANITIZE_FLOAT
, but there are other validation and santation filters. See the below links for a full description.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With