Guest User

Sanitize your POST data in PHP

a guest
Apr 17th, 2014
104
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * @Author: Alexis López (@AlexisThrasher)
  3.  * @Title: Sanitize your POST data in PHP
  4.  *
  5.  * Example: Data received via POST method.
  6.  * Array
  7.  * (
  8.  *    [input1] => <a href = 'bad_site.php'>Link</a>
  9.  *    [input2] => 2
  10.  *    [input3] => Abc
  11.  *    [input4] => 'Hello'
  12.  *    [input5] => 0.343
  13.  * )
  14.  */
  15.  
  16. $mysqli = new mysqli ('server', 'user', 'password', 'bd'); //Connection to MySQL
  17. $cleanedData = array(); //[] in PHP >= 5.3
  18.  
  19. $cleanedData = array_map(function($data) use ($mysqli){
  20.     return strip_tags($mysqli->real_escape_string($data));
  21. }, $_POST);
  22.  
  23. echo implode(', ', $cleanedData); //Link, 2, Abc, \'Hello\', 0.343
RAW Paste Data