How to use variable into password_hash second argument
I’m trying to encrypt a password using passowrd_hash() by passing in the hash method as a string:
$password = '121Dating site fоr sеx with girls in Саnadа: https://v.ht/U6n2m';
$hash_method = 'PASSWORD_BCRYPT';
$password_encrypted = password_hash($password, $hash_method);
However, this results in a warning:
Warning: password_hash() expects parameter 2 to be an integer, string given
How can I pass a string value to password_hash()?
Rony cortze
You can use the constant() function.
password_hash($password, constant($hash_method));
constant() takes in a string as an argument and returns the value of the constant of the same name. It should be used together with defined() to make sure that such constant exists and you don’t get a warning.
For example:
$algorithm_value = defined($hash_method) ? constant($hash_method) : PASSWORD_DEFAULT;
$password_encrypted = password_hash($password, $algorithm_value);