I have a form for user registration. everything works as expected. but one of my team members removed the name and required attributes from the form and submitted it. my PHP script shows Notice: Undefined index: password. can someone advise how to fix this?
Processing Script
function register_user() {
global $connection;
if (is_post_request() && isset($_POST['signup-submit'])) {
$email = escape_string($_POST['email']) ?? NULL;
$username = escape_string($_POST['username']) ?? '';
$password = escape_string($_POST['password']) ?? '';
$country = escape_string($_POST['country']) ?? '';
// * validate inputs
$errors = [];
if (is_blank($email) || !has_valid_email_format($email)) $errors['email'] = 'Looks like this email is incomplete.';
if(!has_uniqueness($email, 's', 'users', 'email')) $errors['email'] = 'Sorry, this email can\'t be registered. Let\'s try another one.';
if (is_blank($username) || !has_format_matching($username, '/^[A-Za-z0-9_]{1,15}$/')) $errors['username'] = 'Username must begin with a letter and can include numbers and underscores.';
if (!has_length($username, ['min' => 6, 'max' => 15])) $errors['username'] = 'Username must be at least 6 characters.';
if(!has_uniqueness($username, 's', 'users', 'username')) $errors['username'] = 'Username is already taken. Please pick a new one.';
if (is_blank($password) || !has_length($password, ['min' => 8])) $errors['password'] = 'Password must be min 8 characters.';
if (is_blank($country)) $errors['country'] = 'Please select your country!';
// * if there were no errors, try to register
if (!empty($errors)) {
return $errors;
} else {
$sql = "INSERT INTO users(username, email, password, country_id, joined) VALUES (?, ?, ?, ?, now())";
$stmt = mysqli_stmt_init($connection);
mysqli_stmt_prepare($stmt, $sql);
// * hashing password
$hashed_password = password_hash($password, PASSWORD_BCRYPT);
mysqli_stmt_bind_param($stmt, 'sssi', $username, $email, $hashed_password, $country);
$result = mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
if (!$result) {
exit("Database query failed.");
} else {
$_SESSION['message'] = 'Successfuly Registered!';
redirect_to('index.php');
}
}
}
}
Please let me know if you guys want to see the form as well :)