O varianta simpla ar fi:
Cod PHP:
class Input{
static public $index;
public function post($index=NULL){
self::$index = $index;
if($index == NULL){
$toret = array();
foreach($_POST as $key => $value){
$toret[$key] = Input::sanitize(trim($value));
}
return $toret;
}
}
public function sanitize($var,$santype=3){
switch($santype){
case 1:
return strip_tags($var);
case 2:
return htmlentities(strip_tags($var), ENT_QUOTES, 'UTF-8');
case 3:
if(!get_magic_quotes_gpc())
return addslashes(htmlentities(strip_tags($var), ENT_QUOTES, 'UTF-8'));
else
return stripslashes(htmlentities(strip_tags($var), ENT_QUOTES, 'UTF-8'));
break;
}
return FALSE;
}
public function valid_input($post,$mandatory){
foreach($mandatory as $key){
if(!isset($post[$key])){
return FALSE;
}
}
return TRUE;
}
}
$post = Input::post();
if($post){
$mandatory = array('field');
if(!valid_input($post,$mandatory)){
return FALSE;
}
}
Javascript nu mai scriu ca s-a scris mai sus.
PS: In cazul in care nu functioneaza clasa de mai sus da un reply, nu am testat-o, am scris-o acum.