What is the best practice for securely storing passwords in PHP when using mysql_connect()?
When storing passwords in PHP and using mysql_connect(), it is important to securely hash the passwords before storing them in the database. This helps protect the passwords in case of a data breach. One common method is to use the password_hash() function to securely hash the password before storing it, and then use password_verify() to compare the hashed password with the user input during login.
// Hashing the password before storing it in the database
$password = 'user_password';
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
// Storing the hashed password in the database
$query = "INSERT INTO users (username, password) VALUES ('user', '$hashed_password')";
$result = mysqli_query($connection, $query);
// Verifying the password during login
$user_input_password = 'user_input_password';
$query = "SELECT * FROM users WHERE username = 'user'";
$result = mysqli_query($connection, $query);
$user = mysqli_fetch_assoc($result);
if (password_verify($user_input_password, $user['password'])) {
// Password is correct
} else {
// Password is incorrect
}