jcadima
6/20/2017 - 7:15 PM

PHP sanitize all form input in one line

PHP sanitize all form input in one line

https://stackoverflow.com/questions/1610582/cleaning-post-variables

<?php
// print the unclean $_POST array 
echo '<pre>';
echo 'plain array<br>';
print_r( $_POST ) ;
echo '</pre>';


$cleanarray = array(); // our empty array 
// filter/sanitize the $_POST array in one go
$cleanarray =  filter_var_array($_POST, FILTER_SANITIZE_STRING)  ;

echo '<pre>';
echo 'clean array<br>';
print_r($cleanarray) ;
echo '</pre>';


// extract()  vars so that they are used as $name, $email, etc
echo 'extract all clean vars: <br>'; 
extract( $cleanarray ) ;

echo 'requirement: ' .  $requirement . '<br>' ; 
echo  'company: ' . $company ;


// ###########  CLEAN ONE BY ONE ############
// clean individual vars, number indexed array
foreach ( $_POST as $key => $value  ) {
	$clean[] =  filter_var($_POST[$key], FILTER_SANITIZE_STRING)  ;
}

echo '<pre>';
echo 'clean vars<br>';
print_r($clean) ;
echo '</pre>';