Encrypt password custom rule in DataMapper and Codeigniter -
i using codeigniter 2.0.3 datamapper orm 1.6.0. integration datamapper in ci has been implemented , works fine, except password encryption.
i have model called users.php
class users
. 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 empty string if (!empty($this->{$field})) { // generate 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 stored in database original password not encrypted.
what doing wrong?
thanks, regards mario
i believe it's salt
class variable missing. in _encrypt
function, call $this->salt
, however, never declare salt in model. think need add salt
variable in class:
// in 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'; // string - helps make password encryption stronger
Comments
Post a Comment