How can the use of salts in password hashing be optimized for better security in PHP applications?

To optimize the use of salts in password hashing for better security in PHP applications, it is recommended to generate a unique salt for each user and store it alongside the hashed password. This adds an extra layer of security by making it harder for attackers to use precomputed rainbow tables. Additionally, using a strong hashing algorithm like bcrypt is crucial for securely hashing passwords.

// Generate a unique salt for each user
$salt = bin2hex(random_bytes(16));

// Combine the password and salt before hashing
$hashed_password = password_hash($password . $salt, PASSWORD_BCRYPT);

// Store the hashed password and salt in the database
$stmt = $pdo->prepare("INSERT INTO users (username, password, salt) VALUES (?, ?, ?)");
$stmt->execute([$username, $hashed_password, $salt]);