Search code examples
phppostsanitization

Override $_POST content


My question is that how can I directly override POST/GET or simply $_REQUEST values in PHP. Most of the scripts I've seen, simply assign a variable to the POST key, and do their desired stuff with it.

I simply need to sanitize the inputs, but my condition is that: I don't have to use the variable for it. For example:

$_POST['name'] = "Example's";

But, I need to override this POST value to:

$_POST['name'] = 'Example\'s';

But, without assigning the variable. In this process, I directly need to edit the value of the POST keys. I tried creating a loop and then doing my filtration process within it but that didn't help me.

Any idea, how to make this run? I'm sorry if I'm not being clear.


Solution

  • Alright, found a fix.

    Realized that it's as simple as:

    function cleanInput($input) {
    
      $search = array(
        '@<script[^>]*?>.*?</script>@si',  
        '@<[\/\!]*?[^<>]*?>@si',           
        '@<style[^>]*?>.*?</style>@siU',   
        '@<![\s\S]*?--[ \t\n\r]*>@'       
      );
    
        $output = preg_replace($search, '', $input);
        return $output;
    }
    
    foreach ($_POST as $key => $value) {
       $_POST[$key] = mysqli_real_escape_string($con,cleanInput($value));
    }