I want to make sure there is nothing substantial that I'm missing, security or performance, on my registration script.
PHP scripts (adduser.php)
Database Structure:
If there is a better way to structure this that I'm not using, please tell me, because I want this to be as high quality as it can be.
PHP scripts (adduser.php)
PHP:
<?php ## Validate Registration ##
session_start();
include("../../scripts/modules.php");
$vars = array(
'username'=>$_POST['username'],
'password'=>$_POST['password'],
'confirmPassword'=>$_POST['confPass'],
'email'=>$_POST['email'],
'confirmEmail'=>$_POST['confEmail'],
'gender'=>$_POST['gender'],
'firstName'=>$_POST['firstName'],
'lastName'=>$_POST['lastName'],
'b-m'=>$_POST['bday-m'],
'b-d'=>$_POST['bday-d'],
'b-y'=>$_POST['bday-y']);
$errors = array();
## Check for empty fields ##
foreach($vars as $values=>$label) {
if(empty($values)) {
$errors[]="$label is invalid";
}
}
## Validate entries ##
## Check for matching passwords ##
if($vars['password'] != $vars['confirmPassword']) {
$errors[]="Passwords do not match.";
}
## Check for matching emails ##
if($vars['email'] != $vars['confirmEmail']) {
$errors[]="Emails do not match.";
}
## Check for valid username ##
$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"; }
## Combine birthday to full mm-dd-yyyy format ##
$bday = $_POST['bday-m'].
"-".
$_POST['bday-d'].
"-".
$_POST['bday-y'];
## Encrypt password ##
$password = hash("sha256",$vars['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,:user,:pass,:email,:gender,:firstName,:lastName,:bday,0)
");
$addUser->bindValue(":user",$_POST['username']);
$addUser->bindValue(":pass",$password);
$addUser->bindValue(":email",$_POST['email']);
$addUser->bindValue(":gender",$_POST['gender']);
$addUser->bindValue(":firstName",$_POST['firstName']);
$addUser->bindValue(":lastName",$_POST['lastName']);
$addUser->bindValue(":bday",$bday);
$addUser->execute();
## 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
);
}
}
?>
Database Structure:
Code:
id
username
password
email
gender
firstname
lastname
birthday
status /* Active */
If there is a better way to structure this that I'm not using, please tell me, because I want this to be as high quality as it can be.