<?php
//Allowed array of parameters
$allowed = array('birth','gender','username','passwordw','email','lang');
$cont=true;
//Loop through the post and check & assign the variables
foreach($_POST as $key=>$value){
if(in_array($key,$allowed) && $value!=''){
//m or f set $cont to false if not
if($key=='gender' && ($value!='m' || $value!='f')){$arr['Code'] = 420; $cont=false;}
//chek if email set $cont to false if not
if($key=='email' && filter_var($value, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED)==true){$arr['Code'] = 419; $cont=false;}
//check pass len set $cont to false if not
if($key=='password' && strlen($value)==32){$arr['Code'] = 423; $cont=false;}
//Assign the variable
$$key=$value;
}else{
//Rouge key in post or value blank
$cont=false;
}
}
//if alls ok
if($cont===true){
$code = genverification();
//PDO connect to database
try {
$dbh = new PDO("mysql:host=localhost;dbname=YOURDB", $dbusername, $dbpassword);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
//Use prepared statement to avoid sql injections
$sth = $dbh->prepare('INSERT into USER
(DATE_BIRTH,GENDER,USER_NAME,PASSWORD,EMAIL,VERIFICATION)
VALUES (:birth,:gender,:username,:password,:email,:code)');
//bind the variables to the parameters
$sth->bindParam(':birth', $birth, PDO::PARAM_STR, strlen($birth));
$sth->bindParam(':gender', $gender, PDO::PARAM_STR, 1);
$sth->bindParam(':username', $username, PDO::PARAM_STR, strlen($username));
$sth->bindParam(':password', $password, PDO::PARAM_STR, strlen($password));
$sth->bindParam(':email', $email, PDO::PARAM_STR, strlen($email));
$sth->bindParam(':code', $code, PDO::PARAM_STR, strlen($code));
$sth->execute();
//Do your mail
require_once("mailer.php");
if (sendmail($email,$link, $lang)){
$arr['Code'] = 200;
}else{
$arr['Code'] = 422;
}
}else{
#Show your errors
}
?>