function RegisterN() {
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if (isset($_POST["password"],$_POST["password1"]) && $_POST["password"]!=$_POST["password1"]) { # Check if password and password1 are same
echo "Your password confirmation is not equal to your password or it missing.";
} else if(!$_POST["email"]){ # Check if email is present
echo "Please enter your email.";
} else if (!$_POST["password"]){ # Check if password is present again
echo "Please enter your password";
} else if (!$_POST["password1"]){ # Check if password1 is present again
echo "Please enter your second password";
} else {
if (strlen ($_POST["password"])>25 || strlen ($_POST["password"])<6) # check password length max 25, min 6
{
echo "Password must be between 6 and 25 characters";
} else {
$dbh = new Database(); # Open database
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); # Small SQL injection security
$stmt = $dbh->prepare('SELECT email FROM members WHERE email=?'); # Check if email exists
$stmt->bindParam(1, $_POST['email'], PDO::PARAM_INT);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
if( ! $rows)
{ # No email found - moving on - If all set - let's register :)
$email = $_POST['email'];
$password = $_POST['password'];
$hash_cost_log2 = 8;
$hash_portable = FALSE;
require("includes/PasswordHash.php"); # Hash password lib
$hasher = new PasswordHash($hash_cost_log2, $hash_portable); # Hashing the password
$hash = $hasher->HashPassword($password);
if (strlen($hash) < 20)
fail('Failed to hash new password');
unset($hasher);
$STH =$dbh->prepare("INSERT INTO members (email,password) VALUES (:email, :password)"); # Registering new user
$STH->bindParam(':email', $email);
$STH->bindParam(':password', $hash);
$STH->execute();
echo "Account created successfully.";
header('Refresh: 2; url=index.php'); # Redirect to index.php in 2 seconds
} else { # email found - closing database - register fail
$dbh=null;
echo('Email already exists.');
}
$dbh=null; # Closing database after register
}
}
} else {
echo"Invalid Token."; # If user access the page without POST
} }
I want to know if I can make it more secure. Before I made the script, I checked other scripts to see how they work, and what security measures they use.
What the script does:
Check if request comes from POST
Check if all fields ware filled ( It's being checked by JS as well, but that can be "cracked" )
Check if email already exists
Hash password
Finally register
In addition, I saw that other scripts are far smaller than mine. Does that happen because they use frameworks?
Update : OOP implemented ( http://pastebin.com/0DKVxrDK ) for check email and register
PasswordHash
class you're using? It seems you're not salting the hash thus making it irresponsibly weak, but I can't know for sure without having more information. \$\endgroup\$