使用未定义的常量PASSWORD

使用未定义的常量PASSWORD

本文介绍了PHP警告:在php 7.3中使用password_hash()时,使用未定义的常量PASSWORD_ARGON2ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近通过Plesk的Web GUI安装了PHP 7.3.6,用于Web应用程序的开发副本,因为我打算将生产环境从php 7.0更新到7.3.我决定借此机会将我们的密码哈希从PBKDF2升级到Argon2ID,因为PHP核心已经包含了它.我很惊讶地收到一条警告,指出PASSWORD_ARGON2ID常量是未定义的,因为我知道它是在PHP 7.3.0中添加的.

I recently installed PHP 7.3.6 through Plesk's web GUI for a development copy of a web app, as I intend to update our production environment from php 7.0 to 7.3. I decided to take the opportunity to upgrade our password hashing from PBKDF2 to Argon2ID since the PHP core has it already included. I was surprised to get a warning stating that the PASSWORD_ARGON2ID constant is undefined, since I understand it was added in php 7.3.0.

我尝试搜索此错误的任何实例,而我发现唯一相关的是Laravel论坛中这篇不详细的帖子:

I tried searching for any instance of this error and the only thing I found that was relevant was this undetailed post in a Laravel forum:

https://laracasts.com/discuss/channels/laravel/use-of-undefined-constant-password-argon2id-assumed-password-argon2id?page=1

该应用程序托管在与MediaTemple共享的vps上. Centos 7,使用nginx作为Apache的反向代理.它是用于运行7.3.6的开发的子域,与运行应用程序的生产版本7.0.33的主域并排运行.

The application is hosted on a shared vps with MediaTemple. Centos 7, using nginx as a reverse proxy over Apache. It is a subdomain for development running 7.3.6 along side the main domain which is running the production version of the app, 7.0.33.

$this->password = password_hash('password123', PASSWORD_ARGON2ID, array('time_cost' => 10, 'memory_cost' => '2048k', 'threads' => 6));

我希望已定义PASSWORD_ARGON2ID常量,但报告为未定义:

I expected the PASSWORD_ARGON2ID constant to be defined but it was reported as undefined:

Use of undefined constant PASSWORD_ARGON2ID - assumed 'PASSWORD_ARGON2ID' (this will throw an Error in a future version of PHP)

推荐答案

仅当PHP已在Argon2支持下进行编译时,此算法才可用.- password_hash

如果要在可用时使用它,我建议使用defined进行检查,否则回退到默认算法.

If you want to use it whenever it is available, I would recommend to check with defined or else fallback to a default algorithm.

if(defined('PASSWORD_ARGON2ID')) {
    $hash = password_hash('password123', PASSWORD_ARGON2ID, array('time_cost' => 10, 'memory_cost' => '2048k', 'threads' => 6));
} else {
    $hash = password_hash('password123', PASSWORD_DEFAULT, array('time_cost' => 10, 'memory_cost' => '2048k', 'threads' => 6));
}

这篇关于PHP警告:在php 7.3中使用password_hash()时,使用未定义的常量PASSWORD_ARGON2ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 18:28