Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [filter_input] Problem z użyciem
Forum PHP.pl > Forum > PHP
starach
Opakowałem sobie funkcje filtrów w klasę Filter. Zdziwiłem się testują ponieważ zarówno klasa jak i zwykłe funkcje zwracają mi wartość null.
Kod testowy:
  1. <?php
  2. $_POST = array('product_id' => 'libgd<script>');
  3.  
  4. $args = array('product_id' => FILTER_SANITIZE_ENCODED);
  5.  
  6. $out = Filter::inputArray(Filter::INPUT_POST, $args);
  7. var_dump($out);
  8. $out = filter_input_array(INPUT_POST, $args);
  9. var_dump($out);
  10.  
  11. $out = Filter::input(Filter::INPUT_POST, 'product_id', Filter::FILTER_SANITIZE_ENCODED);
  12. var_dump($out);
  13. $out = filter_input(INPUT_POST, 'product_id', FILTER_SANITIZE_ENCODED);
  14. var_dump($out);
  15.  
  16. var_dump($_POST);
  17. ?>
Odpowiedź:
Cytat
null

null

null

null

array
'product_id' => string 'libgd<script>' (length=13)
Przypuszczam że funkcje filter_* nie pobierają danych bezpośrednio z tablic super-globalnych. Jak w takim razie mam przetestować te funkcje, unikając pisania formularza do nich?
- To pytanie wynika bardziej z ciekawości niż ogromnej komplikacji, bo i tak zaraz napiszę formularz testowy. tongue.gif

edit>
No tak jak myślałem. Przez formularz, funkcje odpowiadają poprawnie.
pinochet
Czy ja jestem ślepy?Czy nie zamiesciles kodu klasy ?
starach
A po co? Moje pytanie nie dotyczy klasy tylko funkcji filter_* i tego skąd pobierają one dane.
Historyjka z klasą jest tylko tłem problemu.
empathon
Mistrzu...

  1. <?php
  2. $_POST['product_id']  = 'libgd<script>';
  3. ?>


Edit:
Sprawdź sobie czy może klasa korzysta z $HTTP_POST_VARS albo co jeszcze gorzej z register globals
Mize
Drugi argument jest raczej podstawiany w metodzie klasy Filter.
starach
Cytat(empathon @ 21.10.2008, 16:11:53 ) *
Mistrzu...

  1. <?php
  2. $_POST['product_id']  = 'libgd<script>';
  3. ?>


Edit:
Sprawdź sobie czy może klasa korzysta z $HTTP_POST_VARS albo co jeszcze gorzej z register globals
Przecież to dokładnie to samo co ja zrobiłem, żeby przypisać wartość do pola super-globalnej tablicy $_POST, tylko że inaczej zapisane. Poza tym test zerżnąłem z manual'a PHP. http://pl2.php.net/manual/pl/function.filter-input-array.php
Cytat
/* data actually came from POST
$_POST = array(
'product_id' => 'libgd<script>',
'component' => '10',
'versions' => '2.0.33',
'testscalar' => array('2', '23', '10', '12'),
'testarray' => '2',
);
*/


I tak jak napisałem w pierwszym poście. Moja klasa Filter to tylko obiektowe opakowanie wbudowanych w PHP (od 5.2) funkcji filter_* ( np. filter_input )
  1. <?php
  2. class Filter extends Filter_Constants
  3. {
  4.    /**
  5.      * Checks if variable of specified type exists
  6.      * Representation of filter_has_var() function
  7.      * @param integer $type
  8.      * @param string $variable_name
  9.      * @return boolean
  10.      */
  11.    public static function hasVar($type, $variable_name)
  12.    {
  13.        return filter_has_var($type, $variable_name);
  14.    }
  15.    /**
  16.      * Returns the filter ID belonging to a named filter
  17.      * Representation of filter_id() function
  18.      * @param string $filtername
  19.      * @return integer or null
  20.      */
  21.    public static function id($filtername)
  22.    {
  23.        return filter_id($filtername);
  24.    }
  25.    /**
  26.      * Gets external variables and optionally filters them
  27.      * Representation of filter_input_array() function
  28.      * @param integer $type
  29.      * @param mixed $definition
  30.      * @return mixed
  31.      */
  32.    public static function inputArray($type, $definition = null)
  33.    {
  34.        return filter_input_array($type, $definition);
  35.    }
  36.    /**
  37.      * Gets a specific external variable by name and optionally filters it
  38.      * Representation of filter_input() function
  39.      * @param integer $type
  40.      * @param string $variable_name
  41.      * @param integer $filter
  42.      * @param mixed $options
  43.      * @return mixed
  44.      */
  45.    public static function input($type, $variable_name, $filter = null, $options = null)
  46.    {
  47.        if($filter !== null)
  48.        {
  49.            if($options !== null)
  50.            {
  51.                $out = filter_input($type, $variable_name, $filter, $options);
  52.            } else {
  53.                $out = filter_input($type, $variable_name, $filter);
  54.            }
  55.        } else {
  56.            $out = filter_input($type, $variable_name);
  57.        }
  58.        return $out;
  59.    }
  60.    /**
  61.      * Returns a list of all supported filters
  62.      * Representation of filter_list() function
  63.      * @return array
  64.      */
  65.    public static function getList()
  66.    {
  67.        return filter_list();
  68.    }
  69.    /**
  70.      * Gets multiple variables and optionally filters them
  71.      * Representation of filter_var_array() function
  72.      * @param array $data
  73.      * @param mixed $definition
  74.      * @return mixed
  75.      */
  76.    public static function variableArray($data, $definition = null)
  77.    {
  78.        if($definition === null)
  79.        {
  80.            $out = filter_var_array($data);
  81.        } else {
  82.            $out = filter_var_array($data, $definition);
  83.        }
  84.        return $out;
  85.    }
  86.    /**
  87.      * Filters a variable with a specified filter
  88.      * Representation of filter_var() function
  89.      * @param mixed $variable
  90.      * @param integer $filter
  91.      * @param mixed $options
  92.      * @return mixed
  93.      */
  94.    public static function variable($variable, $filter = null, $options = null)
  95.    {
  96.        return filter_var($variable, $filter, $options);
  97.    }
  98. }
  99. ?>
Wszystko mi zaczęło działać kiedy do testowania wykorzystałem prosty formularz, wysyłający jedno pole metodą post.
Stąd moja ciekawość. W jaki sposób funkcje filter_* dostają się do wartości z tablic super-globalnych. Skoro funkcja nie widzi wartości zmiennej, jeśli ją przypiszę wewnątrz skryptu. Czyli na przykład tak jak podał empathon.
empathon
Cytat(orglee @ 21.10.2008, 17:15:19 ) *
Przecież to dokładnie to samo co ja zrobiłem, żeby przypisać wartość do pola super-globalnej tablicy $_POST, tylko że inaczej zapisane.


Przyjrzyj się dokładniej. To co robiłeś może w tym wypadku ma ten sam efekt ale różnica jest znacząca (czepiam się).

http://pl2.php.net/manual/pl/function.filt...array.php#84299
http://pl2.php.net/manual/pl/ref.filter.php#71898
starach
Dobra linki do komentarzy pomogły, ten aspekt już pojąłem.

Jednak nadal nie rozumiem gdzie tutaj jest różnica:
  1. <?php
  2. $_POST = array('product_id' => 'libgd<script>');
  3. $_POST['product_id'] = 'libgd<script>';
  4. ?>
? blinksmiley.gif

- No dobra oprócz tego, że w moim nadpisujemy całą tablicę. Ale chyba nie ma to wpływu na sposób w jakim operuje filter_*.
empathon
To tak jakbys przy zmianie prezydenta wybijal cala administracje (hehe).
Zly nawyk, tak nie wypada.

Jasne, ze to test ale gdybys cos takiego napisal w produkcyjnym kodzie pewnego pieknego pieknego dnia moglbys stracic kilka godzin na debugging. Obaj to wiemy.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.