简体   繁体   English

尝试为bcrypt哈希算法指定成本基准时出现解析错误?

[英]I get a parse error, when trying to specify cost benchmark for bcrypt hashing algorithm?

Can't figure out why i get a parse/syntax error in my code, it works on my localhost server, but when i upload it to my webhost i get a syntax error: "unexpected '[' on line 3". 无法弄清楚为什么我的代码中会出现解析/语法错误,该错误在我的本地主机服务器上有效,但是当我将其上传到我的网络主机时,我得到了语法错误:“第3行出现意外的'['”。

I have a paste-link here: http://codepad.org/26K93MPG 我在这里有一个粘贴链接: http : //codepad.org/26K93MPG

Here's my code: 这是我的代码:

<?php

$options = [
    'cost' => 9,
];

$unhashedPassword = 'testingtesting';
$hashedPassword = password_hash($unhashedPassword, PASSWORD_BCRYPT, $options);

echo $unhashedPassword . "<br>";
echo $hashedPassword;

?>

FIXED! 固定! Here's how i fixed it, and a short explanation about what went wrong: 这是我修复问题的方式,以及发生问题的简短说明:

Firstly, My localhost server's PHP Version was 5.6.4, and my production server's PHP Version was 5.3.29. 首先,我的本地主机服务器的PHP版本是5.6.4,生产服务器的PHP版本是5.3.29。 Since the password_hash() function was first introduced in Version 5.3.7, its obvious that it didnt work. 由于password_hash()函数是在5.3.7版中首次引入的,因此很明显它没有起作用。

Secondly, the array syntax that i was using was only allowed in PHP Version 5.3 or older. 其次,我使用的数组语法仅在PHP 5.3或更早版本中允许。 But the old syntax didnt make it work either, but that was because the password_hash() function wasnt implemented in PHP at that time either. 但是旧的语法也没有使它起作用,那是因为那个时候也没有在PHP中实现password_hash()函数。

To fix both problems: 要解决这两个问题:

1) Go to https://github.com/ircmaxell/password_compat/blob/master/lib/password.php 1)转到https://github.com/ircmaxell/password_compat/blob/master/lib/password.php

2) Put this library file into your folder, and include password.php in your login procedure. 2)将该库文件放入文件夹中,并在登录过程中包含password.php。

3) change the array syntax to this, it's nice and neat: 3)将数组语法更改为此,它很简洁:

<?php
include("password_hash_compatibility.php");

$unhashedPassword = 'testingtesting';
$hashedPassword = password_hash($unhashedPassword, PASSWORD_BCRYPT, array("cost" => 9));

echo $unhashedPassword . "<br>";
echo $hashedPassword;

?>

Your production server is running PHP 5.3 or older and thus does not offer array shorthand syntax which was introduced in PHP 5.4 . 您的生产服务器运行的是PHP 5.3或更早版本,因此不提供PHP 5.4引入的数组速记语法。 Just use the "old" array syntax for backwards compatibility: 只需使用“旧”数组语法即可向后兼容:

$options = array(
    'cost' => 9,
);

This is why you should always make sure your development environment matches your production environment as closely as possible. 这就是为什么您应始终确保开发环境与生产环境尽可能匹配的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM