CodeIgniter Forums
Safely parsing complex permutations of $_POST data - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Safely parsing complex permutations of $_POST data (/showthread.php?tid=29001)



Safely parsing complex permutations of $_POST data - El Forum - 03-27-2010

[eluser]antonumia[/eluser]
hi all,

I have a complex form which posts to ci for processing.

My problem is that there will be various permutations of fields sent for final processing at the moment there are 10-20 initial premutations with up to 10 subpermutations.

Can anyone suggest a safe way of dealing with variable fields in the $_POST data without having to write a endless switch statements dealing with every permutation.

thanks

anton


Safely parsing complex permutations of $_POST data - El Forum - 03-27-2010

[eluser]Tominator[/eluser]
Hello,
you can make function like this:

Code:
function MakeMeSave(&$input) {
   if(is_array($input)) {
      array_walk_recursive($input,"SaveValue");
   } else {
      safetyValue($input);
   }
}
function SaveValue(&$value) {
    
   if (get_magic_quotes_gpc()) {
      $value = stripslashes($value);
   }
  
   $value = htmlspecialchars($value);
   $value = str_replace("'", "'", $value);
   $value = str_replace("\\", "\\\\", $value);
   $value = str_replace("%", "%", $value);
   $value = trim($value);
}

And then in your code you can call just:
Code:
MakeMeSave($POST);

It will work correctly in PHP5. If would you like to make max-lenght limit, you can just add line.