Problem with $ _POST, $ _ GET ... with double dollar

3

I have this code to validate security:

$tipos = array('$_POST','$_GET','$_COOKIE','$_SESSION','$_FILES');
foreach($tipos as $tipo){
    if ( isset($$tipo) && !empty($$tipo) ) {
        foreach ($$tipo as $key => $item) {
            $tipo[$key] = $this->security->xss_clean($item);
        }
    }
}

It gives me error of undefined variable, I know I could validate it one by one, but is there solution like this?

    
asked by Alex Alberich 27.09.2018 в 16:55
source

1 answer

2

In the second foreach you should use $$tipo[$key] instead of $tipo[$key] and your array types should not have $ . It should look like this:

$tipos = array('_POST','_GET','_COOKIE','_SESSION','_FILES');
foreach($tipos as $tipo){
    if ( isset($$tipo) && !empty($$tipo) ) {
        foreach ($$tipo as $key => $item) {
            $$tipo[$key] = $this->security->xss_clean($item);
        }
    }
}
    
answered by 27.09.2018 / 18:54
source