So something to this effect:
Registration Script (addUser.php)
PHP Code:
<?php ## Validate Registration ##
session_start();
## Base Modules ##
include("../../scripts/modules.php");
## Registration Modules ##
include("../../scripts/registration_modules.php");
$vars = array(
'username'=>$_POST['username'],
'password'=>$_POST['password'],
'confirmPassword'=>$_POST['confPass'],
'email'=>$_POST['email'],
'confirmEmail'=>$_POST['confEmail'],
'gender'=>$_POST['gender'],
'fullName'=>$_POST['fullname'],
'shortName'=>$_POST['shortname'],
'b-m'=>$_POST['bday-m'],
'b-d'=>$_POST['bday-d'],
'b-y'=>$_POST['bday-y']);
$errors = array();
## Revised: ##
## Run Authentication ##
checkEmpty($vars);
matching($vars['password'],$vars['confirmPassword'],"Passwords");
matching($vars['email'],$vars['confirmEmail'],"Emails");
varify($username,$password);
## If any errors ##
if($errors) {
userError($errors);
echo "<a href=\"../../index.php?q=users/register\">Try Again</a>";
}
## Otherwise continue ##
else {
try {
$addUser = $db->prepare("
INSERT INTO users(id,username,password,email,gender,firstname,lastname,birthday,status)
VALUES(0,?,?,?,?,?,?,?,0)
");
$addUser->execute(array($_POST['username'],$password,$_POST['email'],$_POST['gender'],$_POST['firstname'],$_POST['lastname'],$bday));
## Start Session ##
$_SESSION['loggedin'] = $_POST['username'];
## Redirect to welcome page ##
header("Location:../../index.php?q=users/welcome");
} catch(PDOException $e) { $entry = uniqID();
reportError(
$e->getMessage(),
"Cannot add user to database. If you feel you have reached this in error, please contact the administrator and reference this ID: $entry",
"../../logs/errors.log",
"adduser.php",
$entry
);
}
}
?>
Registration Modules (registration_modules.php)
PHP Code:
<?php
## Modules for Registration ##
## Check for empty fields ##
function checkEmpty($array) {
foreach($vars as $values=>$label) {
if(empty($values)) {
$errors[]="$label is invalid";
}
}
}
## Matching Values ##
function matching($value1,$value2,$input) {
if($value1 != $value2) {
$errors[]="$input"."s do not match.";
}
}
## Varify information ##
function varify($username,$password) {
$getUsername = $db->prepare("
SELECT username
FROM users
WHERE username = :name");
$getUsername->bindValue(":name",$_POST['username']);
$getUsername->execute();
if($getUsername->rowcount() !== 0) { $errors[]="Username is taken"; }
$password = hash("sha256",$password);
}
## Combine birthday to full mm-dd-yyyy format ##
$bday = $_POST['bday-m'].
"-".
$_POST['bday-d'].
"-".
$_POST['bday-y'];
?>