I am using Codeigniter 2.0.3 with DataMapper ORM 1.6.0. Integration DataMapper in CI has been implemented successfully and everything works fine, except password encryption.
I have model called users.php
with class Users
. I also have set validation rules:
var $validation = array(
array(
'field' => 'password',
'label' => 'Password',
'rules' => array('required', 'trim', 'unique', 'min_length' => 5, 'encrypt'),
),
array(
'field' => 'email',
'label' => 'Email Address',
'rules' => array('required', 'trim', 'valid_email')
)
);
My encrypt function in validation rule:
function _encrypt($field)
{
// Don't encrypt an empty string
if (!empty($this->{$field}))
{
// Generate a random salt if empty
if (empty($this->salt))
{
$this->salt = md5(uniqid(rand(), true));
}
$this->{$field} = sha1($this->salt . $this->{$field});
}
}
MySQL users
table structure:
id
email
password
last_login
created_on
I simple create new user via controller:
$u = new Users();
$u->email = 'mario1@mario-design开发者_高级运维.info';
$u->password = 'mario';
$u->save();
EDIT 1:
Full code of users.php
model here.
User successfully stored in the database but with original password not encrypted.
What am I doing wrong?
Thanks, Regards Mario
I believe it's that your salt
class variable is missing. In your _encrypt
function, you call $this->salt
, however, you never declare salt in the model. I think you need to add a salt
variable in the class:
// in your Users model
<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');
class Users extends DataMapper {
var $table = 'users';
var $has_one = array('profile');
var $salt = 'B1471tU77IK1411'; // any string - helps to make password encryption stronger
I solved problem with upgrading DataMapper to 1.8.x version. Now, it works just fine!
精彩评论