dongyan1625
2016-06-02 06:48
浏览 33
已采纳

codeigniter url加密不起作用

<a href="<?php echo base_url().'daily_report/index/'.$this->encrypt->encode($this->session->userdata('employee_id')) ?>">

i have encrypted the above url using the codeigniter encrypt i set the encryption key in codeigniter config file

$config['encryption_key'] = 'gIoueTFDwGzbL2Bje9Bx5B0rlsD0gKDV';

and i called in the autoload

$autoload['libraries'] = array('session','form_validation','encrypt','encryption','database');

when the ulr(href) load into the url it look like this

http://localhost/hrms/daily_report/index/FVjGcz4qQztqAk0jaomJiAFBZ/vKVSBug1iGPQeKQCZ/K7+WUE4E/M9u1EjWh3uKTKeIhExjGKK1dJ2awL0+zQ==

but the url is not decoded, and i;m not getting the employee_id it shows empty.

public function index($employee_id) {
        $save_employee_id = $employee_id;
        // decoding the encrypted employee id
        $get_employee_id = $this->encrypt->decode($save_employee_id);
        echo $employee_id; // answer: FVjGcz4qQztqAk0jaomJiAFBZ
        echo "<br>";
        echo $get_employee_id; // is display the null
        echo "<br>";

        exit();
        // get the employee daily report
        $data['get_ind_report'] =           $this->daily_report_model->get_ind_report($get_employee_id);
        // daily report page
        $data['header'] = "Daily Report";
        $data['sub_header'] = "All";
        $data['main_content'] = "daily_report/list";
        $this->load->view('employeelayout/main',$data);
    }

complete url(3) is

FVjGcz4qQztqAk0jaomJiAFBZ/vKVSBug1iGPQeKQCZ/K7+WUE4E/M9u1EjWh3uKTKeIhExjGKK1dJ2awL0+zQ==

it shows only

FVjGcz4qQztqAk0jaomJiAFBZ

i tried to change in the

$config['permitted_uri_chars'] = 'a-zA-Z 0-9~%.:_\-@=+';

by / in the permitted uri chars but it throwing error So, i need to encryption the $id in the url using the codeigniter encrypt class and decrypt in the server side to get the actual $id, So that i fetch data from the DB. any help would be appreciated

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douyinghuo8874 2016-06-02 07:03
    已采纳

    You have to extend encryption class and avoid the / to get it working. Place this class in your application/libraries folder. and name it as MY_Encrypt.php.

    class MY_Encrypt extends CI_Encrypt
    {
        /**
         * Encodes a string.
         * 
         * @param string $string The string to encrypt.
         * @param string $key[optional] The key to encrypt with.
         * @param bool $url_safe[optional] Specifies whether or not the
         *                returned string should be url-safe.
         * @return string
         */
        function encode($string, $key="", $url_safe=TRUE)
        {
            $ret = parent::encode($string, $key);
    
            if ($url_safe)
            {
                $ret = strtr(
                        $ret,
                        array(
                            '+' => '.',
                            '=' => '-',
                            '/' => '~'
                        )
                    );
            }
    
            return $ret;
        }
    
        /**
         * Decodes the given string.
         * 
         * @access public
         * @param string $string The encrypted string to decrypt.
         * @param string $key[optional] The key to use for decryption.
         * @return string
         */
        function decode($string, $key="")
        {
            $string = strtr(
                    $string,
                    array(
                        '.' => '+',
                        '-' => '=',
                        '~' => '/'
                    )
            );
    
            return parent::decode($string, $key);
        }
    }
    
    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题