How can a unique salt be generated and stored for each user in a PHP application?

To generate and store a unique salt for each user in a PHP application, you can use the PHP function uniqid() to create a unique identifier for each user. This unique identifier can then be used as the salt for hashing passwords. You can store this salt in the user's database record along with their other information.

// Generate a unique salt for the user
$salt = uniqid();

// Hash the password using the salt
$hashed_password = hash('sha256', $password . $salt);

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