I made a class in my way of learning php5 OOP that clean global vars like GET , POST .., SERVER at the developer's choice.
in a file MyCleanerClass.php put the following code.
Usage:
I hope that helps someone....
in a file MyCleanerClass.php put the following code.
PHP Code:
class XpGlobalVarsCleaner {
private $Ints = array();
private $Floats = array();
private $Bools = array();
private $NoTypes = 0;
private $NoProcesses = 0;
public function SetType($type,$val){
$this->NoTypes++;
switch(strtolower($type)){
case 'int':
if(is_array($val))
$this->Ints = $val;
else
$this->Ints[] = $val;
break;
case 'float':
if(is_array($val))
$this->Floats = $val;
else
$this->Floats[] = $val;
break;
case 'bool':
if(is_array($val))
$this->Bools = $val;
else
$this->Bools[] = $val;
break;
// .... //
default: echo 'Error at SetType(\''.$type.'\',Array of values\')['.$this->NoTypes.']'; break;
}
}
private function CleanProcess(&$data){
foreach($data as $key=>$val){
if(in_array($key,$this->Ints)){
$data[$key] = (int)$val;
}
else if(in_array($key,$this->Floats)){
$data[$key] = (float)$val;
}else if(in_array($key,$this->Bools)){
$data[$key] = (bool)$val;
}else{
$data[$key] = mysql_real_escape_string($val);
}
}
}
public function CleanData($type){
$this->NoProcesses++;
switch(strtolower($type)){
case 'get': $MyArray = &$_GET; break;
case 'post': $MyArray = &$_POST; break;
case 'session': $MyArray = &$_SESSION; break;
case 'cookie': $MyArray = &$_COOKIE; break;
case 'server': $MyArray = &$_SERVER; break;
// .... //
default: echo 'Error at CleanData(\''.$type.'\')['.$this->NoProcesses.']'; break;
}
self::CleanProcess($MyArray);
return;
}
}
PHP Code:
<?php
$_GET['uid'] = 'Just a name';
$_GET['sid'] = '2193asd\'as41312dax16';
$_GET['ok'] = 'a string';
require('MyCleanerClass.php');
$safe = new XpGlobalVarsCleaner;
$safe->SetType('int',array('who','uid','id')); // in array or
$safe->SetType('int','type'); // one by one
$safe->SetType('bool','ok');
$safe->CleanData('get'); // what to clean: get , post, sessions, server .. etc.
// uid must be int , sid a string and ok a boolean
echo $_GET['uid'].' and '.$_GET['sid'].' and '.$_GET['ok']; //output: 0 and 2193asd\'as41312dax16 and 1
?>
Comment