My question is more about design practice, but where should I start the filtering? I figured the class would be the best place for it, but wouldn't that put the instantiated object ($objReg) at risk for code injection using unfiltered POST variables like that?
How do I get around this problem? Is there a better design for this?
Index.php is the handler for all calls. So for example it will call register.php's function register2() (i.e. index.php?action=register2), and the function in-turn instantiates the object as seen below.
Is this safe you think? I'm trying to follow DRY and OOD principles in order to avoid confusion.
main code @ register.php (includes ommited):
function register2(){
$objReg = new registration($_POST['username'],$_POST['password'],$_POST['email']);
$objReg->register();
unset($objReg);
}
clean class @ security.php:
class clean
{
public function process($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
}
registration class @ register.class (register method omitted):
class registration
{
// Registration stuffs
private $regUser = NULL;
private $regPass = NULL;
private $regEmail = NULL;
public function __construct($regName, $regPass, $regEmail)
{
//Set the class properties if they aren't NULL.
if (!empty($regName) AND !empty($regPass) AND !empty($regEmail)){
$objClean = new clean;
$this->regUser = $objClean->process($regName);
$this->regPass = $objClean->process($regPass);
$this->regEmail = $objClean->process($regEmail);
unset($objClean);
}
}
}