I want to secure my page by checking if the value is digital (0,1,2,3) and if it is in the range from 0 to 120. I think ctype_digit
function limits numbers, so can not be passed any negative number. How can I limit the max value in the simplest way?
if (!ctype_digit($_GET['category'] AND ...) die('');
if (!ctype_digit($_GET['category'] > 120) ?
I was thinkig about intval
but it can pass negative numbers.
You might want to take a look at PHP's Data Filtering.
It provides a filter for your task (FILTER_VALIDATE_INT
) which also accepts min_range
and max_range
parameters:
$value = filter_var($_GET['category'], FILTER_VALIDATE_INT, array(
'options' => array(
// An optional default value
'default' => 123,
// Desired validation range
'min_range' => 0,
'max_range' => 120
),
));
// $value is FALSE when validation failed, or an "int" with
// the correct value.
if (!ctype_digit($_GET['category']) || $_GET['category'] > 120) die('')
Basically this says "If it's not a number or if it's larger than 120, stop"
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