Global addslashes isn't working [FIXED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Global addslashes isn't working [FIXED]

Post 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);
		}
	}
}
Last edited by php_wiz_kid on Thu Jun 16, 2005 4:17 pm, edited 1 time in total.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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;
}
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post by php_wiz_kid »

Had reset() in while loop. Oops 8O
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Post 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);
    }
}
Post Reply