Funciones de Filter

Tabla de contenidos

  • filter_has_var β€” Verifica si una variable de un tipo especΓ­fico existe
  • filter_id β€” Devuelve el ID del filtro al que pertenece un filtro con nombre
  • filter_input β€” Toma una variable externa concreta por su nombre y opcionalmente la filtra
  • filter_input_array β€” Obtiene variables externas y opcionalmente las filtra
  • filter_list β€” Devuelve una lista de todos los filtros soportados
  • filter_var β€” Filtra una variable con el filtro que se indique
  • filter_var_array β€” Obtiene mΓΊltiple variables y opcionalmente las filtra
οΌ‹add a note

User Contributed Notes 2 notes

up
3
vojtech at x dot cz ΒΆ
18 years ago
Also notice that filter functions are using only the original variable values passed to the script even if you change the value in super global variable ($_GET, $_POST, ...) later in the script.

<?php
echo filter_input(INPUT_GET, 'var'); // print 'something'
echo $_GET['var']; // print 'something'
$_GET['var'] = 'changed';
echo
filter_input(INPUT_GET, 'var'); // print 'something'
echo $_GET['var']; // print 'changed'
?>

In fact, external data are duplicated in SAPI before the script is processed and filter functions don't use super globals anymore (as explained in Filter tutorial bellow, section 'How does it work?').
up
-1
fumble1 at web dot de ΒΆ
18 years ago
I recommend you to use the FILTER_REQUIRE_SCALAR (or FILTER_REQUIRE_ARRAY) flags, since you can use array-brackets both to access string offsets and array-element -- however, not only this can lead to unexpected behaviour. Look at this example:

<?php
$image
= basename(filter_input(INPUT_GET, 'src', FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW));
// further checks
?>

/script.php?src[0]=foobar will cause a warning. :-(
Hence my recommendation:

<?php
$image
= basename(filter_input(INPUT_GET, 'src', FILTER_UNSAFE_RAW, FILTER_REQUIRE_SCALAR | FILTER_FLAG_STRIP_LOW));
// further checks
?>
To Top