How can PHP's crypt or mcrypt functions be utilized for password security in a web application?

To enhance password security in a web application, PHP's crypt or mcrypt functions can be utilized to securely hash passwords before storing them in a database. By using a strong hashing algorithm and salting the passwords, it makes it more difficult for attackers to crack the passwords even if the database is compromised.

// Generate a random salt
$salt = mcrypt_create_iv(22, MCRYPT_DEV_URANDOM);

// Hash the password using bcrypt with the salt
$hashed_password = crypt($password, '$2a$12$' . $salt);

// Store $hashed_password and $salt in the database