Page 1 of 1

Global addslashes isn't working [FIXED]

Posted: Thu Jun 16, 2005 4:02 pm
by php_wiz_kid
Why isn't this working?

Code: Select all

if(!get_magic_quotes_gpc()) {
	if(is_array($_POST)) {
		while(list($key, $value) = each($_POST)) {
			if(is_array($_POST[$key])) {
				$_POST[$key] = addslashes($value);
			}
			@reset($_POST);
		}
	}
	
	if(is_array($_GET)) {
		while(list($key, $value) = each($_GET)) {
			if(is_array($_GET[$key])) {
				$_GET[$key] = addslashes($value);
			}
			@reset($_GET);
		}
	}
	
	if(is_array($_COOKIE)) {
		while(list($key, $value) = each($_COOKIE)) {
			if(is_array($_COOKIE[$key])) {
				$_COOKIE[$key] = addslashes($value);
			}
			@reset($_COOKIE);
		}
	}
}

Posted: Thu Jun 16, 2005 4:05 pm
by hawleyjr
I'm not sure, but here is a function which does what your trying to do:

Code: Select all

#http://us2.php.net/stripslashes
function addslashes_deep($value)
{

   $value = is_array($value) ?
               array_map('addslashes_deep', $value) :
               addslashes($value);

   return $value;
}

Posted: Thu Jun 16, 2005 4:18 pm
by php_wiz_kid
Had reset() in while loop. Oops 8O

Posted: Thu Jun 16, 2005 4:20 pm
by php_wiz_kid
Fix:

Code: Select all

if(!get_magic_quotes_gpc()) {
    if(is_array($_POST)) {
        while(list($key, $value) = each($_POST)) {
             $_POST[$key] = addslashes($value);
        }
        @reset($_POST);
    }
    
    if(is_array($_GET)) {
        while(list($key, $value) = each($_GET)) {
             $_GET[$key] = addslashes($value);
        }
        @reset($_GET);
    }
    
    if(is_array($_COOKIE)) {
        while(list($key, $value) = each($_COOKIE)) {
          $_COOKIE[$key] = addslashes($value);
        }
        @reset($_COOKIE);
    }
}