php - 如何在php中使用Bcrypt通过注册和登录过程对密码进行加密

标签 php mysql bcrypt

我有一个注册页面,允许用户输入密码之一的信息,因此必须对其进行加密或散列,因此使用 Bcrypt 并且它在注册器上工作

但是当涉及到登录时,我不知道在哪里以及如何使用它
谁能帮助我吗?

注册.php:

require_once('Bcrypt.php'); 

$bcrypt = new Bcrypt(15);
$hash = $bcrypt->hash('$pass1');

//********Insert all the members's input to the database**************//
$query = mysql_query("INSERT INTO members
                      (user_name, first_name, last_name,
                       governorate, district, village,
                       birth_date, email_address,
                       specialization, password, registered_date)
                      VALUES
                      ('$username', '$firstname', '$lastname',
                       '$governorate', '$district', '$village',
                       '$bdate', '$email', '$specialization',
                       ' $hash',  now())")
                      or die("could not insert data");

登录.php

require_once('Bcrypt.php');

$bcrypt = new Bcrypt(15);
$hash = $bcrypt->hash('$pass');
$isGood = $bcrypt->verify('$pass', $hash);

$sql=mysql_query("SELECT user_id, email_address,
                         first_name, user_name
                  FROM members
                  WHERE email_address='$email'AND password= '$hash'
                  LIMIT 1") or die("error in members table");

$login_check = mysql_num_rows($sql);

最佳答案

它对我来说看起来很好,除了我将把login.php查询从WHERE email_address='$email'AND password='$hash'写入到WHERE email_address='$email ' AND 密码='$hash'

确保保存密码散列的列数据类型足够长以包含用户将输入的任何密码。如果数据的额外字节不是问题,则将该列设置为 varchar(255) 以确保 MySQL 在保存哈希值时不会截断哈希值的末尾。

我会检查一些内置的密码学库。它们自 php 5.5 起已更新,如果 bcrypt 无法正常工作,它们可能会为您完成这项工作。

http://www.php.net/manual/en/refs.crypto.php

我过去使用过 md5,没有任何问题,如果你真的担心它 super 安全,请查看 php 内置的 mcrypt 库。您还可以对密码“加盐”,以确保它们的安全。

关于php - 如何在php中使用Bcrypt通过注册和登录过程对密码进行加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16618175/

相关文章:

php - 我如何让 rabbitmq 的客户脚本持续监听

python - npm 安装 bcrypt,gyp 错误! Python

javascript - Node.JS Schema.pre('save) 没有改变数据

node.js - NPM :Cannot find module 'bcrypt'

PHP集群()与compress.zlib ://doesn't work?

php - 在层次结构中首先显示 Div

php - 使用 while 循环回显 mysql_fetch_row 超过 1 行?

php - 过滤mysql数据以进行图表可视化

Mysql 带子查询的左外连接 (wordpress)

javascript - 从 JSON 数组获取列表结果