I have improved my code since the last time asking for its review.
I'd like to know if it is up to the current standards.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
error_reporting(-1);
require '../../config/connect.php';
$con = new mysqli(...$dbCredentials);
$first_name = htmlspecialchars(trim(strip_tags(filter_var($_POST['first_name'],
FILTER_SANITIZE_STRING))));
$last_name = htmlspecialchars(trim(strip_tags(filter_var($_POST['last_name'],
FILTER_SANITIZE_STRING))));
$username = htmlspecialchars(trim(strip_tags(filter_var($_POST['username'],
FILTER_SANITIZE_STRING))));
$email = htmlspecialchars(trim(strip_tags(filter_var($_POST['email'],
FILTER_VALIDATE_EMAIL))));
$pw = '';
$pw2 = '';
$friend_array = ',';
$errors = [];
if (empty($_POST["first_name"])) {
$errors[] = "Fill in first name to sign up";
}
if (empty($_POST["last_name"])) {
$errors[] = "Fill in last name to sign up";
}
if (empty($_POST["username"])) {
$errors[] = "Fill in username to sign up";
}
if (empty($_POST["email"])) {
$errors[] = "Fill in username to sign up";
}
if (empty($_POST["pw"])) {
$errors[] = "Fill in password to sign up";
}
if (empty($_POST["pw2"])) {
$errors[] = "Confirm password to sign up";
}
if (!$errors) {
//An SQL statement template is created and sent to the database
$check_username = $con->prepare("SELECT username FROM users WHERE username=?");
// This function binds the parameters to the SQL query and tells the database what the parameters are.
$check_username->bind_param("s", $_POST['username']);
// the database executes the statement.
$check_username->execute();
$row = $check_username->get_result()->fetch_assoc();
if ($row && $row['username'] == $_POST['username']) {
$_SESSION['error'] = '<b><p style="color: #000000; font-size: 25px; top: 34%;right: 30%;position: absolute;">Username exists</p></b>';
header('Location: ../../register.php');
exit();
}
}
if (!$errors) {
//An SQL statement template is created and sent to the database
$check_email = $con->prepare("SELECT email FROM users WHERE email=?");
// bind_param binds the parameters to the query and tells the DB what the parameters are.
$check_email->bind_param("s", $email);
// the database executes the statement.
$check_email->execute();
$row = $check_email->get_result()->fetch_assoc();
if ($row && $row['email'] == $_POST['email']) {
$_SESSION['error'] = '<b><p style="color: #000000; font-size: 25px; top: 34%;right: 30%;position: absolute;">E-mail exists</p></b>';
header('Location: ../../register.php');
exit();
}
}
if ($_POST['pw'] !== $_POST['pw2']) {
$_SESSION['error'] = '<b><p style="color: #000000; font-size: 25px; top: 34%;right: 30%;position: absolute;">The passwords do not match.</p></b>';
header('Location: ../../register.php');
exit();
}
if (!$errors) {
$pw = password_hash($_POST['pw'], PASSWORD_BCRYPT, array('cost' => 14));
$stmt = $con->prepare("INSERT INTO users (first_name, last_name, username, email, pw, friend_array)
VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssss", $_POST['first_name'], $_POST['last_name'], $_POST['username'],
$_POST['email'], $pw, $friend_array);
$stmt->execute();
$_SESSION['error'] = '<b><p id="loginnow">You can now login.</p></b>';
header('Location: ../../register.php');
exit();
} else {
// The foreach construct provides an easy way to iterate over arrays.
foreach ($errors as $error) {
echo "$error <br /> \n";
}
$_SESSION['error'] = '<b><p style="color: #fff; font-size: 25px; top: 15%;right: 30%;position: absolute;">An error occurred.</p></b>';
header('Location: ../../register.php');
exit();
}