Php 如果密码是在CodeIgniter中的数据库中编码的,如何保存密码?
我是数据库中CodeIgniter的新手。密码字段使用加密密钥进行编码,但当我想要登录时,它与密码不匹配。控制器、视图和模型的名称分别为Hello、login和user_model 以下是我的看法:Php 如果密码是在CodeIgniter中的数据库中编码的,如何保存密码?,php,jquery,html,mysql,codeigniter,Php,Jquery,Html,Mysql,Codeigniter,我是数据库中CodeIgniter的新手。密码字段使用加密密钥进行编码,但当我想要登录时,它与密码不匹配。控制器、视图和模型的名称分别为Hello、login和user_model 以下是我的看法: .文字危险{ 颜色:红色; } 函数myFun(){ var r4=document.getElementById('email')。值; var r5=document.getElementById('password')。值; 如果(r4==“”){ document.getElementBy
.文字危险{
颜色:红色;
}
函数myFun(){
var r4=document.getElementById('email')。值;
var r5=document.getElementById('password')。值;
如果(r4==“”){
document.getElementById('f4').style.display=“block”;
返回false;
}
else if(r5==“”){
document.getElementById('f5').style.display=“block”;
返回false;
}
}
函数myFun4(r){
如果(r!=0){
document.getElementById('f4').style.display=“无”;
}
}
函数myFun5(r){
如果(r!=0){
document.getElementById('f5').style.display=“无”;
}
}
电子邮件
此字段必填
密码
此字段必填
提交
我的控制器是
<?php
class Hello extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url');
$this->load->helper('html');
$this->load->library('image_lib');
$this->load->database();
$this->load->library('form_validation');
$this->load->model('user_model');
$this->load->library('encrypt');
}
function index() {
$this->load->view('login.php');
}
function logindata() {
$f1 = $this->input->post("email");
$f2 = $this->input->post("password");
$encrypt_pwd2 = $this->encrypt->encode($f2);
$this->form_validation->set_rules("email", "Email", "trim|required");
$this->form_validation->set_rules("password", "Password", "trim|required");
if ($this->form_validation->run() == FALSE) {
$this->load->view('login');
} else {
$data = $this->user_model->get_password($f1, $f2);
$plainpassword = $this->encrypt->decode($data);
if ($plainpassword == $f2) {
$this->dataa['posts'] = $this->user_model->userdata($f1, $f2);
$this->load->view('home', $this->dataa);
} else {
$this->load->view('home');
}
}
}
} ?>
这是我的模型
<?php
class User_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database();
$this->load->library('encrypt');
}
function get_password($f1, $f2) {
$this->db->select('password');
$this->db->from('user_detail');
$this->db->where('email', $f1);
return $this->db->get()->result()->row('password');
}
function userdata($f1, $f2) {
$q = $this->db->get_where('user_detail', array('email' => $f1));
if ($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$dataa[] = $row;
}
return is_array($dataa) ? $dataa : array();
}
}
}
?>
我注意到的第一件事是您正在尝试“加密”密码。99.99%的时候,这样做是错误的。你想对密码做的是使用一种专为密码存储而设计的算法 PHP实际上有一个非常有用的库,称为密码哈希() 存储密码时,您希望使用password\u散列函数。下面的代码片段显示了如何将其与密码“password1!”一起使用 这是存储在数据库中的值 尝试验证密码是否正确时,可以使用php password_verify函数。此函数将返回一个布尔值。如果返回true,则表示用户已成功通过身份验证
<?php
$original_hash = SOMETHING // this is the hash you have stored in a database
// in this case, it would be
// $2y$11$q5MkhSBtlsJcNEVsYh64a.aCluzHnGog7TQAKVmQwO9C8xb.t89F.
if (password_verify('password1!', $original_hash)) {
echo 'Successful login';
// do normal login things here
} else {
echo 'invalid password.';
// return an error because they had the wrong password
}
?>
PHP减轻了您的很多负担,您可以使用它安全地处理密码。我注意到的第一件事是您正在尝试“加密”密码。99.99%的时候,这样做是错误的。你想对密码做的是使用一种专为密码存储而设计的算法 PHP实际上有一个非常有用的库,称为密码哈希() 存储密码时,您希望使用password\u散列函数。下面的代码片段显示了如何将其与密码“password1!”一起使用 这是存储在数据库中的值 尝试验证密码是否正确时,可以使用php password_verify函数。此函数将返回一个布尔值。如果返回true,则表示用户已成功通过身份验证
<?php
$original_hash = SOMETHING // this is the hash you have stored in a database
// in this case, it would be
// $2y$11$q5MkhSBtlsJcNEVsYh64a.aCluzHnGog7TQAKVmQwO9C8xb.t89F.
if (password_verify('password1!', $original_hash)) {
echo 'Successful login';
// do normal login things here
} else {
echo 'invalid password.';
// return an error because they had the wrong password
}
?>
PHP减轻了您的许多负担,您可以使用它安全地处理密码。如果您的密码是可检索的,则代码中存在安全漏洞
像“获取密码”这样的功能不应该存在。
密码的加密方式必须确保,您唯一能做的就是检查给定的输入,一旦加密,是否与数据库中存储的内容相似。 @haxim建议使用php库。这可能是一个很好的解决方案——我对它不熟悉 MySQL在MySQL数据库的用户表中列出用户帐户。 每个MySQL帐户都可以分配一个密码,尽管用户表 不存储密码的明文版本,而是存储哈希值 根据它计算 MySQL在客户端/服务器通信的两个阶段使用密码: 当客户机尝试连接到服务器时,会出现初始错误 身份验证步骤,其中客户端必须提供 具有与存储在用户表中的哈希值匹配的哈希值 客户想要使用的帐户 客户端连接后,它可以(如果它有足够的权限) 为用户表中列出的帐户设置或更改密码哈希。 客户端可以通过使用PASSWORD()函数生成 密码哈希,或使用密码生成语句(CREATE 用户、授予或设置密码)
关于保护用户数据的文章很多。这取决于你希望你的网站有多安全。但最基本的规则是密码不可检索,确认有效性的方法是检查加密输入是否与数据库中存储的加密值相似。如果密码可检索,则代码中存在安全漏洞
像“获取密码”这样的功能不应该存在。
密码的加密方式必须确保,您唯一能做的就是检查给定的输入,一旦加密,是否与数据库中存储的内容相似。 @haxim建议使用php库。这可能是一个很好的解决方案——我对它不熟悉 MySQL在MySQL数据库的用户表中列出用户帐户。 每个MySQL帐户都可以分配一个密码,尽管用户表 不存储密码的明文版本,而是存储哈希值 根据它计算 MySQL使用密码
$2y$11$q5MkhSBtlsJcNEVsYh64a.aCluzHnGog7TQAKVmQwO9C8xb.t89F.
<?php
$original_hash = SOMETHING // this is the hash you have stored in a database
// in this case, it would be
// $2y$11$q5MkhSBtlsJcNEVsYh64a.aCluzHnGog7TQAKVmQwO9C8xb.t89F.
if (password_verify('password1!', $original_hash)) {
echo 'Successful login';
// do normal login things here
} else {
echo 'invalid password.';
// return an error because they had the wrong password
}
?>