Please take a look at my code below and tell me if I'm doing the right thing here. My wishes:
Username
- The username must be at least 6 characters wronglong
- The username can't contain any special symbols
Password:
- The password must be at least 8 characters
- The password must contain at least 1 upper- and lowercase letter
- The password must contain at least 1 number
- The password can't contain any special symbols
As you can see I am also using a hash function for the password. It is supposed to be hashed like this for the purpose I'm using it for. Does this effectaffect my code at all?
//password hash function
function l2j_hash($password) {
return base64_encode(pack("H*", sha1(utf8_encode($password))));
}
$pass = mysqli_real_escape_string($db_link, $_POST['password']);
$repass = mysqli_real_escape_string($db_link, $_POST['repeat_password']);
$user = mysqli_real_escape_string($db_link, $_POST['name']);
$uppercase = preg_match('@[A-Z]@', $pass);
$lowercase = preg_match('@[a-z]@', $pass);
$number = preg_match('@[0-9]@', $pass);
if(!$uppercase || !$lowercase || !$number || !ctype_alnum($password) || strlen($password) < 8) {
echo "The password must contain at least 1 lowercase letter, 1 uppercase letter and 1 number.";
echo "The password can't contain special symbols.";
} else if ($pass != $repass) {
echo "Passwords do not match";
} else if ($user == '' && strlen($user) < 6 && !ctype_alnum($user)) {
echo "The User ID must be at least 6 characters long and can't contain special symbols.";
} else {
$pass_hash = l2j_hash($pass);
$db_add = mysqli_query( "INSERT INTO `accounts` VALUES ('$user', '$pass_hash', '0', '0', '')" ) or die( 'Error: '.mysqli_error() );
echo 'Account created';
}