0

In my previous project i was created a custom encryption function to login. How can i use it in CI. here is my code

function sha_password($username,$password){
$username = strtoupper($username);
$password = strtoupper($password);
return SHA1($username.':'.$password);
}

and i was called like that to get encrypted password

$password = strtoupper(sha_password($username,$password));

how can i do it to work in CI? :?

1 Answer 1

2

you can place it in various places:

  1. a model - if you have a model for a user, $user->getEncryptedPassword();

  2. a library - in my project i have libuser that has the encryption function, so i call it by $this->libuser->encrypt_password();

  3. a controller (MY_Controller for example) - create a function and call it by $this->encrypt_user_password(..)

  4. just drop it in some of the files that will always be loaded, in config or something

  5. if you don't plan on changing it, just do $encpass = sha1(strtoupper($username.':'.$password)); although i wouldn't go there.

options 1 and 2 are most recommended

Sign up to request clarification or add additional context in comments.

5 Comments

Yes i have. This my user model '<?php class Membership_model extends CI_Model{ function sha_password($username,$password){ $username = strtoupper($username); $password = strtoupper($password); return SHA1($username.':'.$password); } function validate(){ $this->db->where('username',$this->input->post('username')); $this->db->where('sha_pass_hash',$this->input->post('password')); $query = $this->db->get('account'); if($query->num_rows == 1){ return TRUE; } } }' i don't know how to use sha_password function ooz of i'm newbie in CI. Btw thanks for your answer
in your case, in order to call the sha_password() method, you need an instance of Membership_model ($m->sha_password()), or you could make this method static (Membership_model::sha_password()). either that, or create a library with this function
when i use like that i got a fatal error call to undifined function 'function validate(){ $this->db->where('username',$this->input->post('username')); $this->db->where('sha_pass_hash',sha_password($this->input->post('password'))); $query = $this->db->get('account'); if($query->num_rows == 1){ return TRUE; } }' how can i use it?
here is my controller to validate 'function validate_credentials(){ $this->load->model('membership_model'); $query = $this->membership_model->validate(); //if the user's credentials validated.... if($query){ $data = array( 'username' => $this->input->post('username'), 'is_logged_in' => true ); $this->session->set_userdata($data); redirect('site/members_area'); }else{ //if not validate login page again $this->index(); } }'
i did not understand where you placed it. did you put it in the model? if so, within the model you can call it by $this->sha_password() and outside the model, you need the instance, ie $m->sha_password(). i think where you're going is $this->membership_model->sha_password()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.