如何使用库加密codeigniter从数据库加密ID值?

时间:2019-07-16 03:32:35

标签: php codeigniter encryption datatables

*注意:我正在使用ignited datatables的{​​{1}}库来构建codeigniter

这是我的没有query的模型代码:

id encryption

它工作正常。

接下来,我想使用codeigniter加密库对data-id值进行加密。

这是我尝试的方法:

class Madmin extends CI_Model{
    function __construct(){
        parent::__construct();
    }

     function getIdtData($select,$table,$where) { 
        $this->datatables->add_column('check', '<input type="checkbox" class="data-check"  value="$1">','idkeunggulan');
        $this->datatables->select(''.$select.'');
        $this->datatables->from($table);
        $this->datatables->join('ikonmaster', 'ikonmaster.id = keunggulan.idikonmaster','left outer');
        $this->datatables->where($where);
        $this->datatables->add_column('tool', '<button type="button" class="edit btn btn-outline-success " data-id="$1"  onClick="editData()"><i class="fa fa-edit"></i> edit</button>','idkeunggulan');

        return $this->datatables->generate();
    }

加密工作正常,但是$this->datatables->add_column('tool', '<button type="button" class="edit btn btn-outline-success " data-id='.$this->encrypt->encode('$1').' onClick="editData()"><i class="fa fa-edit"></i> edit</button>','idkeunggulan'); 返回value,但返回的是$1 不完全 id value来自database。 / p>

我该如何解决此问题?

2 个答案:

答案 0 :(得分:0)

或设置密钥,请转到application / config / config.php,打开文件并设置:

$config['encryption_key'] = "YOUR KEY"; 

application / Controller.php(加密代码)

$this->load->library('encrypt');
$this->encrypt->encode('put value');

解码代码

$this->encrypt->decode();

答案 1 :(得分:0)

我发现了我的问题,在检查了Ignited Datatables库之后,我才意识到$ 1不是可变符而是匹配替换。

但是我最终使用了没有库Ignited Datatables的另一种方式,因为直到现在我还不知道如何使用上面的代码加密data-id。

下面是我使用的代码。 这是我的控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Coba extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->helper('url');
        $this->load->library(array('encrypt','session', 'datatables'));
        $this->load->model('Madmin');
        $this->load->database();
    }
    public function getIdt()
    {
        $table = 'keunggulan';
        $column_order = array('','nama','');    ////set column field database for datatable orderable *include blank column
        $column_search = array('nama'); //set column field database for datatables are searchable
        $order = array('idcoba' => 'desc'); // default order
        $join = '';     //join table
        $where = '';    //where condition

        $list = $this->Madmin->get_datatables($table,$column_order,$column_search,$order,$join,$where);
        $data = array();
        $no = $_POST['start'];
        foreach ($list as $person) {
            $no++;
            $row = array();
            $row[] = '<input type="checkbox" class="data-check"  value="'.$person->idkeunggulan.'">';
            $row[] = '';
            $row[] = $person->nama;

            //add html for action

            $row[] = '<button type="button" class="edit btn btn-outline-success " data-id="'.$this->encrypt->encode($person->idkeunggulan).'"  onClick="editData()"><i class="fa fa-edit"></i> edit</button>
                  <button type="button"class="hapus mr-2 btn-icon btn-icon-only btn btn-outline-danger " data-id="'.$this->encrypt->encode($person->idkeunggulan).'" onClick="deleteData()"><i class="fa fa-trash-alt"></i> Hapus</button>';

            $data[] = $row;
        }

        $output = array(
                        "draw" => $_POST['draw'],
                        "recordsTotal" => $this->Madmin->count_all(),
                        "recordsFiltered" => $this->Madmin->count_filtered(),
                        "data" => $data,
                );
        //output to json format
        echo json_encode($output);
    }

这是我的模特:

class Madmin extends CI_Model{
    var $table = '';
    var $column_order = ''; //set column field database for datatable orderable
    var $column_search = ''; //set column field database for datatable searchable just firstname , lastname , address are searchable
    var $order = ''; // default order 
    var $join = '';
    var $where = '';


    function __construct(){
        parent::__construct();
    }

    private function _get_datatables_query()
    {

        $this->db->select('*');
        $this->db->from($this->table);

        if($this->join ==''){

        }
            else if($this->join !=''){
                $this->db->join($this->join[0],$this->join[1],$this->join[2]);  // query join with array join column 0,1,2
            }

        if($this->where ==''){

        }
            else if($this->where !=''){
                $this->db->where($this->where); // query where condition
            }

        $i = 0;

        foreach ($this->column_search as $item) // loop column 
        {
            if($_POST['search']['value']) // if datatable send POST for search
            {

                if($i===0) // first loop
                {
                    $this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
                    $this->db->like($item, $_POST['search']['value']);
                }
                else
                {
                    $this->db->or_like($item, $_POST['search']['value']);
                }

                if(count($this->column_search) - 1 == $i) //last loop
                    $this->db->group_end(); //close bracket
            }
            $i++;
        }

        if(isset($_POST['order'])) // here order processing
        {
            $this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
        } 
        else if(isset($this->order))
        {
            $order = $this->order;
            $this->db->order_by(key($order), $order[key($order)]);
        }
    }

    function get_datatables($table,$column_order,$column_search,$order)
    {
         $this->table = $table;
         $this->column_order = $column_order; //set column field database for datatable orderable
         $this->column_search = $column_search; //set column field database for datatable searchable just firstname , lastname , address are searchable
         $this->order = $order; // default order 
         $this->join ='';
         $this->where ='';

        $this->_get_datatables_query();
        if($_POST['length'] != -1)
        $this->db->limit($_POST['length'], $_POST['start']);
        $query = $this->db->get();
        return $query->result();
    }

    function count_filtered()
    {
        $this->_get_datatables_query();
        $query = $this->db->get();
        return $query->num_rows();
    }

    public function count_all()
    {
        $this->db->from($this->table);
        return $this->db->count_all_results();
    }

    public function get_by_id($id)
    {
        $this->db->from($this->table);
        $this->db->where('idcoba',$id);
        $query = $this->db->get();

        return $query->row();
    }