CodeIgniter 3.1.3发送加密的URL变量偶尔不起作用

时间:2017-02-07 02:03:05

标签: php codeigniter url encryption

我正在尝试使用新的CI加密库通过url传递加密的user_ID变量,将变量传递到控制器并传入模型以基于传递的变量值运行查询。问题是有时它是有效的,有时它不是。我按照用户指南中的说明设置了加密密钥,我已经在自动加载中加载了库:

$key = bin2hex($this->encryption->create_key(16));

并将密钥设置为config.php:

$config['encryption_key'] = bin2hex('some_bin2hex_numbers_and_letters');

查看(获取要传递的变量):

<div class="col-sm-8">
    <?php foreach ($user_list as $row) {
        $ID = $row['user_ID'];
        $UID = $this->encryption->encrypt($ID);
        $username = $row['user_Username'];
        $firstname = $row['user_FirstName'];
        $lastname = $row['user_LastName']; 
    ?>

    <a href="<?php echo base_url()?>Admin/User?uid=<?php echo $UID;?>">
        <h2 style="font-family:'Comic Sans MS', cursive;\"> <?php echo $username." (".$firstname." ".$lastname.") </h2> <br />" ?>
    </a>

    <?php } ?>
</div>

控制器:

public function User() {
    $ID  = $this->input->get('uid');
    $UID = $this->encryption->decrypt($ID);
    $data['user'] = $this->Admin_model->getUserData($UID);
    $this->load->view('pages/admin/admin_header');  
    $this->load->view('pages/admin/admin_user_data', $data);    
    $this->load->view('templates/footer2'); 
}

model(获取传递的变量并返回一行):

public function getUserData($UID) {
    $query = $this->db->query("SELECT * FROM `tbl_user` WHERE `tbl_user`.`user_ID`='$data'");
    $row = $query->row();
    return $row;
}

查看admin_user_data(显示传递变量结果的视图):

<h1>
    <?php 
    echo "User ID: ".$user->user_ID."<br />";
    echo "Username: ".$user->user_Username."<br />";
    echo "First Name: ".$user->user_FirstName."<br />";
    echo "Last Name: ".$user->user_LastName."<br />";
    ?>
</h1>

1 个答案:

答案 0 :(得分:0)

实际上这种方法适合我。

用于加密

$this->load->library('encryption'); 

$this->encryption->initialize(
array(
       'cipher' => 'aes-256',
       'mode' => 'ctr',
       'key' => '4lWC8cegO92vhTRbiRy21sh90TwFz6DR' // A random key generated by visiting http://randomkeygen.com/
     )
);   

$enc_id = $this->encryption->encrypt($id);

用于解密

    $this->load->library('encryption'); 

    $this->encryption->initialize(
    array(
            'cipher' => 'aes-256',
            'mode' => 'ctr',
            'key' => '4lWC8cegO92vhTRbiRy21sh90TwFz6DR' // Same key you used for encryption
        )
    );  

    $dec_id = $this->encryption->decrypt($enc_id);

上述方法每次都适合我。如果您遇到任何问题,请在下方发表评论。