How can PHP developers securely store passwords in databases using hashing and salts?
To securely store passwords in databases using hashing and salts, PHP developers can use functions like password_hash() to hash passwords and generate unique salts for each user. By combining the hashed password with the unique salt, passwords become more secure as they are less susceptible to dictionary attacks or rainbow table attacks.
// Generate a random salt
$salt = bin2hex(random_bytes(16));
// Combine password with salt and hash using bcrypt
$hashed_password = password_hash($password . $salt, PASSWORD_BCRYPT);
// Store hashed password and salt in the database
// Ensure the database column size is sufficient to store the hashed password and salt