【问题标题】:codeigniter url encrypt not workingcodeigniter url加密不起作用
【发布时间】:2016-10-01 18:18:29
【问题描述】:
<a href="<?php echo base_url().'daily_report/index/'.$this->encrypt->encode($this->session->userdata('employee_id')) ?>">

我已经使用 codeigniter encrypt 加密了上面的 url 我在 codeigniter 配置文件中设置了加密密钥

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

我调用了自动加载

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

当 ulr(href) 加载到 url 中时,它看起来像这样

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

但是 url 没有被解码,而且我没有得到它显示为空的 employee_id。

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);
    }

完整的 url(3) 是

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

只显示

FVjGcz4qQztqAk0jaomJiAFBZ

我试图改变

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

by / 在允许的 uri 字符中 但它抛出错误 因此,我需要使用 codeigniter 加密类加密 url 中的 $id 并在服务器端解密以获取实际的 $id,以便我从数据库中获取数据。任何帮助将不胜感激

【问题讨论】:

  • 您可能想在URL parameter encryption 上阅读这篇文章;特别是如果您使用的是旧的 CI_Encrypt 类。

标签: php codeigniter encryption


【解决方案1】:
FVjGcz4qQztqAk0jaomJiAFBZ/vKVSBug1iGPQeKQCZ/K7+WUE4E/M9u1EjWh3uKTKeIhExjGKK1dJ2awL0+zQ==

表演

FVjGcz4qQztqAk0jaomJiAFBZ

如果您仔细查看您的网址,您会发现在显示的结果之后有一个 '/' 。现在,之后的任何字符串都将被视为另一个段。因此它无法解码。 在这种情况下,加密库将不起作用。

要么停止通过 URL 传递它,要么使用另一种不同的技术 base_encode()。

希望有帮助

【讨论】:

  • “停止通过 URL 传递” - 有什么建议吗?
【解决方案2】:

您必须扩展加密类并避免使用/ 才能使其正常工作。将此类放在您的 application/libraries 文件夹中。并将其命名为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);
    }
}

【讨论】:

  • 这很棒,你能帮我在控制器和 href 中使用它吗?请…………
  • 您无需执行任何操作,只需放置文件即可使用
  • 感谢您的帮助,感谢您抽出宝贵时间提出问题并得到,它运行良好
【解决方案3】:

这是因为字符“/”是 html uri 分隔符的一部分。相反,您可以通过在将其附加到 url 之前对您的加密输出字符串进行 rawurlencoding 避免 html url 中的该字符来解决它。

\编辑:

我尝试了 rawurlencode,但无法获得正确的输出。

使用这段代码终于成功了。 定义两个函数:

function hex2str( $hex ) {
  return pack('H*', $hex);
}

function str2hex( $str ) {
  return array_shift( unpack('H*', $str) );
}

然后使用调用 str2hex 并将加密的用户 ID 传递给它,以将加密的字符串转换为十六进制代码。 反转该过程以获取正确的字符串,以便您可以解密它。

我能够正确编码和解码:

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

到:

"46566a47637a3471517a7471416b306a616f6d4a694146425a2f764b56534275673169475051654b51435a2f4b372b57554534452f4d397531456a576833754b544b65496845786a474b4b31644a3261774c302b7a513d3d"

但网址会变得相当长。

【讨论】:

  • 感谢您的帮助,您可以用更好的方法来做,在 helper 文件夹中创建一个帮助文件,并将上面@karman 中的代码复制到帮助文件中,然后在任何您想要的地方使用它。就是这样,你也可以编写自己的助手类.....
  • 是的,他的解决方案绝对是优雅的!
猜你喜欢
  • 1970-01-01
  • 2017-11-10
  • 2019-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-06
  • 1970-01-01
相关资源
最近更新 更多