⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

how to insert password with encrypt method



JpunkLy
  • profile picture
  • Member

Posted 17 June 2013 - 19:36 PM

1. create a function encrypt()

public function encrypt($post)
	{
		$pass = $this->input->post('password');
		$post['password'] = sha1($pass);

		return $this->db->insert('tb_user',$post);

	}

on variable $post, you can change a method encryption with, md5($post); or sha1($post); and some else method encryption.

I use SHA1....because security...and it's up to you.

 

2. implements callback_password

public function user()
	{
		$cek = $this->session->userdata('logged_in');
		if(empty($cek))
		{
			header('location:'.base_url().'login');
		}
		else
		{
			$crud = new grocery_CRUD();
			$crud->set_theme('datatables');

			$crud->set_table('tb_user');
			
			
			$crud->set_subject('User');
			
                        $crud->field_type('password','password');
                        
			//This Callback Encryption
                        $crud->callback_before_insert('password',array($this,'encrypt'));
                        $output = $crud->render();
			$this->_admin($output);

                }
       }

regards....!!!!  :D  :P It's So Simple... :D


ssfthurber
  • profile picture
  • Member

Posted 18 June 2013 - 18:52 PM

so if i use your code to store user auth data and i create a login function 

then i can have a complete solution by using victor'a code, yes?

victor's code

 

 
class test extends CI_Controller
{
var $ses_data;
function __construct()
{
parent::__construct();
if ($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$this->user_data = array('username' => $session_data['username'],
'id' => $session_data['userid'],
'permisos' => $session_data['userperms']);
}
else
{
redirect('login/index/', 'refresh');
}
}
function manager()
{
//other code
$output = $crud->render();
$output->ses_data = $this->ses_data;
$this->load->view('home_view.php',$output);
}
}

JpunkLy
  • profile picture
  • Member

Posted 19 June 2013 - 10:31 AM

I use a function authentication user login to default so simple login code..

 

controller/login.php

<?php if(!defined('BASEPATH')) exit ('No Direct script access allowed');

class Login extends CI_Controller{

    function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->helper('url');
        $this->load->model('login_model');
    }

    public function index()
    {
        $cek = $this->session->userdata('logged_in');
        if(empty($cek))
        {
            
            //create Attribut Form
            $form['username'] = array('name' => 'username',
                'id' => '',
                'type' => 'text',
                'class' => 'span12',
                'autocomplete' => 'off',
                'placeholder' => 'Please Insert Username ...'
            );
            
            $form['password'] = array('name' => 'password',
                'id' => '',
                'type' => 'password',
                'class' => 'span12',
                'autocomplete' => 'off',
                'placeholder' => 'Please Insert Password....'
            );

            

            $this->form_validation->set_rules('username', 'Username','required');
            $this->form_validation->set_rules('password','Password','required');

            if($this->form_validation->run() == FALSE)
            {
                $this->load->view('login',$form);
            }
            else
            {
                $user = $this->input->post('username');
                $pass = $this->input->post('password');

                $this->login_model->getLoginData($user, $pass);
            }
        }
        else
        {
            header('location:'.base_url().'login/logout');
         
        }
    }

    public function logout()
    {
        $cek = $this->session->userdata('logged_in');
        if(empty($cek))
        {
            header('location:'.base_url().'login');
        }
        else
        {
            $this->session->sess_destroy();
            header('location:'.base_url().'login');
        }
    }

    

}

and to load a login_model.php in : models/login_model.php

public function getLoginData($user,$pass)
	{
		$usr = mysql_real_escape_string($user);
		$pwd = sha1(mysql_real_escape_string($pass));
		$cek_login = $this->db->get_where('table_login', array('username' => $usr, 'password' => $pwd));
		if(count($cek_login->result()) > 0)
		{
			foreach ($cek_login->result() as $login) {
				if($login->jabatan == 'Administrator' && $login->stats== 'Active')
				{
					foreach($cek_login->result() as $log)
					{
						$sess_data['logged_in'] = 'YesSayaTelahLogin';
						$sess_data['username'] = $log->username;
						$sess_data['nama_karyawan'] = $log->full_name;
						$sess_data['id_kar'] = $log->id_employee;
						$sess_data['jabatan'] = $log->jabatan;
						$this->session->set_userdata($sess_data);
					}
					header('location:'.base_url().'administrator/employee');
				}
				
				else
				{
					$this->session->set_flashdata('result_login','Username Not Registered');
					header('location:'.base_url().'login');


				}
				
			}
		}
		else
		{
			$this->session->set_flashdata('result_login','Your Username Or Password It's Wrong..!!');
			header('location:'.base_url().'login');
		}
	} 

I Use a function login in default login_model ... not use a function in grocery_CRUD()


ssfthurber
  • profile picture
  • Member

Posted 19 June 2013 - 12:43 PM

nice!