【发布时间】:2023-03-03 01:26:01
【问题描述】:
我创建了一个可以下载电影和电视剧的小网站。在那里我添加了一个用户注册和登录系统。我使用xampp最新版本(php版本5.6.19)查看网站。在注册 php 文件中,我添加了密码盐技术。它在 xampp 中完美运行。在我托管我的网站后,它没有工作。密码盐不会生成并存储在数据库中。但用户名、电子邮件和密码已成功进入数据库。
注册.php
<?php
require_once 'core/init.php';
if(Input::exists()){
$user = new User();
$salt = Hash::salt(32);
try{
$user->create(array(
'uname' => Input::get('uname'),
'mail' => Input::get('mail'),
'pass' => Hash::make(Input::get('pass'), $salt),
'salt' => $salt
));
Session::flash('home', 'You have registered successfully now you can log in !');
Redirect::to('index.html');
}catch(Exception $e){
die($e->getMessage());
}
}
?>
哈希.php
<?php
class Hash{
public static function make($string, $salt = ''){
return hash('sha256', $string . $salt);
}
public static function salt($length){
return mcrypt_create_iv($length);
}
public static function unique(){
return self::make(uniqid());
}
}
用户.php
<?php
class User{
private $_db,
$_data,
$_sessionName,
$_cookieName,
$_isLoggedIn;
public function __construct($user = null){
$this->_db = DB::getInstance();
$this->_sessionName = Config::get('session/session_name');
$this->_cookieName = Config::get('remember/cookie_name');
if(!$user){
if(Session::exists($this->_sessionName)){
$user = Session::get($this->_sessionName);
if($this->find($user)){
$this->_isLoggedIn = true;
}else{
//process logout
}
}
}else{
$this->find($user);
}
}
public function create($fields = array()){
if(!$this->_db->insert('users', $fields)){
throw new Exception('There was a problem creating an account !');
}
}
public function find($user = null){
if($user){
$field = (is_numeric($user)) ? 'id' : 'uname';
$data = $this->_db->get('users', array($field, '=', $user));
if($data->count()){
$this->_data = $data->first();
return true;
}
return false;
}
}
public function login($username = null, $password = null, $remember = false ){
if(!$username && !$password && $this->exists()){
Session::put($this->_sessionName, $this->data()->id);
}else{
$user = $this->find($username);
if($user){
if($this->data()->pass === Hash::make($password, $this->data()->salt)){
Session::put($this->_sessionName, $this->data()->id);
if($remember){
$hash = Hash::unique();
$hashCheck = $this->_db->get('users_session', array('user_id', '=', $this->data()->id));
if(!$hashCheck->count()){
$this->_db->insert('users_session',array(
'user_id' => $this->data()->id,
'hash' => $hash
));
}else{
$hash = $hashCheck->first()->hash;
}
Cookie::put($this->_cookieName, $hash, Config::get('remember/cookie_expiry'));
}
return true;
}
}
}
return false;
}
public function exists(){
return (!empty($this->_data)) ? true : false;
}
public function logout(){
$this->_db->delete('users_session', array('user_id', '=', $this->data()->id));
Session::delete($this->_sessionName);
Cookie::delete($this->_cookieName);
}
public function data(){
return $this->_data;
}
public function isLoggedIn(){
return $this->_isLoggedIn;
}
}
什么可能导致问题...
【问题讨论】:
-
很难说。我们可以从 user.php 中看到代码吗?
-
“密码盐没有生成和存储”——这是两个不同的症状。它是生成的而不是存储的?
-
在
register.php中将 user + salt 实例化放在 try/catch 块中,是否有错误消息?您在数据库中的密码字段是什么数据类型?它的大小可能小于传递的值,导致它没有被保存。 -
@dchayka 数据类型为 varchar,密码字段长度为 64,salt 为 32
-
请考虑切换到专用函数password_hash() 和password_verify()。您的函数对于散列密码是不安全的。上述函数会自行创建一个安全的盐并将其包含在哈希值中,因此无需将其单独存储在数据库中。