Php 如何在第二个参数中使用变量?

Php 如何在第二个参数中使用变量?,php,constants,password-hash,Php,Constants,Password Hash,我正在尝试使用password\u hash()对密码进行散列,方法是使用字符串传入散列算法名称: $password = '121@121'; $hash_method = 'PASSWORD_BCRYPT'; $password_encrypted = password_hash($password, $hash_method); 但是,这会导致警告: 警告:password_hash()要求参数2为整数,字符串给定 如果我想动态确定算法,例如从数据库中设置的配置变量,我如何将字符串值传递

我正在尝试使用
password\u hash()
对密码进行散列,方法是使用字符串传入散列算法名称:

$password = '121@121';
$hash_method = 'PASSWORD_BCRYPT';
$password_encrypted = password_hash($password, $hash_method);
但是,这会导致警告:

警告:password_hash()要求参数2为整数,字符串给定


如果我想动态确定算法,例如从数据库中设置的配置变量,我如何将字符串值传递给
password\u hash()。PASSWORD_BCRYPT是一个人性化的数值版本,它省去了记忆十亿个数字及其对应值的麻烦

编辑的信息

Dharman found-表示从PHP7.4开始,加密类型的常量值将不再是数值

密码哈希算法标识符现在是可为空的字符串,而不是整数

PASSWORD_DEFAULT was int 1; now is NULL
PASSWORD_BCRYPT was int 1; now is string '2y'
PASSWORD_ARGON2I was int 2; now is string 'argon2i'
PASSWORD_ARGON2ID was int 3; now is string 'argon2id'
只需删除引号,就可以开始了:

<?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(-1);

    $password = '121@121';
    $hash_method = PASSWORD_BCRYPT;
    $password_encrypted = password_hash($password, $hash_method);

    echo '<pre>'. print_r($password_encrypted, 1) .'</pre>';
';
发生这种情况是因为
密码\u BCRYPT
是-而不是字符串。PASSWORD_BCRYPT是一个人性化的数值版本,它省去了记忆十亿个数字及其对应值的麻烦

编辑的信息

Dharman found-表示从PHP7.4开始,加密类型的常量值将不再是数值

密码哈希算法标识符现在是可为空的字符串,而不是整数

PASSWORD_DEFAULT was int 1; now is NULL
PASSWORD_BCRYPT was int 1; now is string '2y'
PASSWORD_ARGON2I was int 2; now is string 'argon2i'
PASSWORD_ARGON2ID was int 3; now is string 'argon2id'
只需删除引号,就可以开始了:

<?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(-1);

    $password = '121@121';
    $hash_method = PASSWORD_BCRYPT;
    $password_encrypted = password_hash($password, $hash_method);

    echo '<pre>'. print_r($password_encrypted, 1) .'</pre>';
'; 您可以使用该功能

/**
 * (PHP 5 &gt;= 5.5.0, PHP 5)<br/>
 *
 * Creates a password hash.
 * @link http://www.php.net/manual/en/function.password-hash.php
 * @param string $password The user's password.
 * @param int $algo A <a href="http://www.php.net/manual/en/password.constants.php" class="link">password algorithm constant</a>  denoting the algorithm to use when hashing the password.
 * @param array $options [optional] <p> An associative array containing options. See the <a href="http://www.php.net/manual/en/password.constants.php" class="link">password algorithm constants</a> for documentation on the supported options for each algorithm.
 * If omitted, a random salt will be created and the default cost will be used.
 * <b>Warning<b>
 * <p>
 * The salt option has been deprecated as of PHP 7.0.0. It is now
 * preferred to simply use the salt that is generated by default.
 * </p>
 * @return string|bool Returns the hashed password, or FALSE on failure.
 * @since 5.5.0
 */
function password_hash ($password, $algo, $options = null) {}
constant()
接受字符串作为参数,并返回同名常量的值。它应该与一起使用,以确保此类常量存在,并且不会收到警告

例如:

您可以使用该函数

/**
 * (PHP 5 &gt;= 5.5.0, PHP 5)<br/>
 *
 * Creates a password hash.
 * @link http://www.php.net/manual/en/function.password-hash.php
 * @param string $password The user's password.
 * @param int $algo A <a href="http://www.php.net/manual/en/password.constants.php" class="link">password algorithm constant</a>  denoting the algorithm to use when hashing the password.
 * @param array $options [optional] <p> An associative array containing options. See the <a href="http://www.php.net/manual/en/password.constants.php" class="link">password algorithm constants</a> for documentation on the supported options for each algorithm.
 * If omitted, a random salt will be created and the default cost will be used.
 * <b>Warning<b>
 * <p>
 * The salt option has been deprecated as of PHP 7.0.0. It is now
 * preferred to simply use the salt that is generated by default.
 * </p>
 * @return string|bool Returns the hashed password, or FALSE on failure.
 * @since 5.5.0
 */
function password_hash ($password, $algo, $options = null) {}
constant()
接受字符串作为参数,并返回同名常量的值。它应该与一起使用,以确保此类常量存在,并且不会收到警告

例如:


您应该使用
password\u BCRYPT
设置
password\u hash
的第二个参数
$algo
,而不是字符串
'password\u BCRYPT'

/**
*(PHP5=5.5.0,PHP5)
* *创建密码哈希。 *@linkhttp://www.php.net/manual/en/function.password-hash.php *@param string$password用户的密码。 *@param int$algoa表示对密码进行哈希运算时要使用的算法。 *@param array$options[可选]包含选项的关联数组。有关每个算法支持的选项的文档,请参阅。 *如果省略,将创建一个随机salt,并使用默认成本。 *警告 * *自PHP7.0.0起,salt选项已被弃用。现在是 *首选只使用默认生成的盐。 *

*@return string | bool返回哈希密码,失败时返回FALSE。 *@自5.5.0以来 */ 函数密码\u散列($password,$algo,$options=null){}
您应该使用
密码\u哈希
设置第二个参数
$algo
,而不是字符串
'password\u BCRYPT'

/**
*(PHP5=5.5.0,PHP5)
* *创建密码哈希。 *@linkhttp://www.php.net/manual/en/function.password-hash.php *@param string$password用户的密码。 *@param int$algoa表示对密码进行哈希运算时要使用的算法。 *@param array$options[可选]包含选项的关联数组。有关每个算法支持的选项的文档,请参阅。 *如果省略,将创建一个随机salt,并使用默认成本。 *警告 * *自PHP7.0.0起,salt选项已被弃用。现在是 *首选只使用默认生成的盐。 *

*@return string | bool返回哈希密码,失败时返回FALSE。 *@自5.5.0以来 */ 函数密码\u散列($password,$algo,$options=null){}
密码\u BCRYPT
是一个常量而不是字符串。不需要引用Fine,但是我如何从数据库中获取它呢?我想问题是为什么数据库中保存了一个常量名?@DawidWalczyk你留下的评论是什么意思?“例如,如何从数据库中获取它?”。如果将此类数据存储在数据库中,为什么不使用
constant($variable)
呢?
PASSWORD\u BCRYPT
是常量而不是字符串。不需要引用Fine,但是我如何从数据库中获取它呢?我想问题是为什么数据库中保存了一个常量名?@DawidWalczyk你留下的评论是什么意思?“例如,如何从数据库中获取数据?”。如果将此类数据存储在数据库中,为什么不使用
constant($variable)