困惑的实施Bcrypt

时间:2014-10-21 23:48:39

标签: php mysql bcrypt

这是我的代码,但是在使用Bcrypt进行哈希密码时我很困惑:

<?php
session_start();
include_once('bcrypt.php');

$db_host = 'localhost';
$db_user = '';
$db_pass = '';
$db_name = 'credentials';

if (!isset($_POST['userName']))
{
    echo 'You did not enter a valid username.';
    exit;
}

if (!isset($_POST['pass']))
{
    echo 'You did not enter a valid password.';
    exit;
}

$con = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($con->connect_error)
{
    die('Connect Error (' . $con->connect_errno . ') ' . $con->connect_error);
}

$sql = "SELECT userName, pass FROM `Members` WHERE userName = ?";
if (!$result = $con->prepare($sql))
{
    die('Query failed: (' . $con->errno . ') ' . $con->error);
}

if (!$result->bind_param('s', $_POST['userName']))
{
    die('Binding parameters failed: (' . $result->errno . ') ' . $result->error);
}

if (!$result->execute())
{
    die('Execute failed: (' . $result->errno . ') ' . $result->error);
}

$result->store_result();
if ($result->num_rows == 0)
{
    die('There is no such username in our database.');
}

$result->bind_result($db_username, $db_password);
$result->fetch();
$result->close();
$con->close();

$bcrypt = new Bcrypt(15);
if ($bcrypt->verify($pass, $db_password))
{
    $_SESSION['userName'] = $db_username;
    header('location:index.html');
    exit;
}
else
{
    echo 'Incorrect username or password.';
}

点击提交按钮登录后,我收到的错误是:

Warning: include_once(bcrypt.php): failed to open stream: No such file or  
directory in C:\Users\Julian Buscema\Desktop\Subpost.me\htdocs\connectivity-  
login.php on line 3
Warning: include_once(): Failed opening 'bcrypt.php' for inclusion  
(include_path='.;C:\Users\Julian Buscema\Desktop\Subpost.me\php\PEAR') in  
C:\Users\Julian Buscema\Desktop\Subpost.me\htdocs\connectivity-login.php on line 3
You did not enter a valid username.

我相信我错过了bcrypt.php,但我从未和Bcrypt合作过,所以我不太确定从哪里开始。

1 个答案:

答案 0 :(得分:0)

问题出在您的包含文件中,根目录中缺少bcrypt.php,您不需要像这样使用Bcrypt,因为Bcrypt已在php中实现,您可以改为使用函数。