URL中使用CodeIgniter加密的参数

Jer*_* Wu 1 php encryption url codeigniter codeigniter-2

嗨,我正在使用CodeIgniter制作应用程序。

我想将数字ID加密为加密的字符串。

http://example.com/post/6

http://example.com/post/v4th54u654khi3f23of23ir2h398eh2xi012

我尝试了内置的加密库

$ this->加密->编码(6)

但是它会为每次页面加载生成不同的加密字符串,这不是我想要的,我希望永久链接就像Youtube视频ID一样。

我需要加密的字符串也可以解密。

Ran*_*yab 5

$config['encryption_key'] = "Test@123";在配置文件中设置了吗

要么

你必须在字符串后面传递密钥

$this->encrypt->encode(6, 'Test@123')

$this->encrypt->decode(6, 'Test@123')

我为此扩展了核心库

使用创建文件名MY_Encrypt.php并将此文件放入application \ libraries

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

class MY_Encrypt extends CI_Encrypt
{

  function encode($string, $key = "", $url_safe = TRUE) {
      $ret = parent::encode($string, $key);

      if ($url_safe) {
          $ret = strtr($ret, array('+' => '.', '=' => '-', '/' => '~'));
      }

      return $ret;
  }

  function decode($string, $key = "") {
      $string = strtr($string, array('.' => '+', '-' => '=', '~' => '/'));

      return parent::decode($string, $key);
  } 

}  

?>
Run Code Online (Sandbox Code Playgroud)

现在您可以使用

$this->encrypt->encode(6)

$this->encrypt->decode(6)

这将有相同的结果。