in PHP

Magic Quotes – Quickly and Safely Removing Slashes

I have seen loads of ways of looping though GET, POST and COOKIE and stripping slashes, but I finally think I found one I am happy with.
The benefits it offers include…

  • Only runs if magic quotes are turned on
  • Much faster than anything I have used before
  • Clean and tidy
	if (get_magic_quotes_gpc())
	{
		$in = array(&$_GET, &$_POST, &$_COOKIE);
		while (list($k,$v) = each($in))
		{
			foreach ($v as $key => $val)
			{
				if (!is_array($val))
				{
					$in[$k][$key] = stripslashes($val);
					continue;
				}
				$in[] =& $in[$k][$key];
			}
		}
		unset($in);
	}