Codeigniter加密没有斜杠

时间:2016-09-07 03:12:10

标签: php codeigniter

我知道这可能看起来像这个问题重复:Ignore slash while using encryption in Codeigniter。但我仍然没有得到答案。

我想将加密的电子邮件名称作为URL发送到他们的电子邮件帐户。 然后解密该URL以搜索我的数据库中是否存在该电子邮件名称以允许该电子邮件进入我的系统。

问题是:

  1. 如果我在加密后使用 urlencode base64_encode ,则在解密后始终会导致空值搜索数据库。我认为是因为加密值总是在变化。
  2. 如果我使用临时加密,它可能有(“/”)字符。
  3. 如果我只使用编码而没有加密,则可能允许电子邮件名称访问我的系统。
  4. 最后,我找到了一些Ignore Slash while using encryption in codeigniter - GitHub

    但它给了我这个错误:未定义的属性:CI_Loader :: $ my_encrypt

    我不知道我做错了什么,我已经:

    1. 将班级名称首字母大写。
    2. 使用与类名相同的文件名。 (也大写)
    3. 将扩展名更改为 CI_Encryption ,因为已弃用加密类。
    4. 在所有方法之前插入public function __construct() {parent::__construct();}
    5. 将文件放在应用程序/库中。
    6. 加载库$this->load->library('my_encrypt');
    7. 使用$this->my_encrypt->encode($key);加载方法,这是给我一个错误的行。
    8. 我知道这听起来像是一个简单的错误,但我也在使用另一个第三方库,但它根本没有给我一个错误。

      任何人都可以帮我找到那里的错误/遗失步骤吗?

      更新 -  在我在控制器中加载库之前,我想先查看结果。但即使我把代码放在控制器中,它也没有给我任何改变。这是代码:

          $key = 'example@gmail.com';
          $this->load->library('my_encrypt');
      
          $segment = $this->my_encrypt->encode($key);  
          echo $segment; 
          echo ( $this->my_encrypt->decode($segment) );
      

      更新: 修复库代码以使用CI_Encryption库进行扩展

2 个答案:

答案 0 :(得分:3)

你加载了图书馆吗?在应用程序库中将librabry命名为MY_Encrypt.php

<?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
     */
    public function __construct() {
        parent::__construct();
    }

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

现在调用加密库并使用加密类而不是my_encrypt

$key='Welcome';
    $this->load->library('encrypt');
    $key1= $this->encrypt->encode($key);

    echo $key1;

答案 1 :(得分:2)

已修复以扩展CI_Encryption库,抱歉打扰。 :)     

class MY_Encrypt extends CI_Encryption
{
   /**
 * 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
 */
public function __construct() {
parent::__construct();
}

function encode($string)
{
    $ret = parent::encrypt($string);

    if ( !empty($string) )
    {
        $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)
{
    $string = strtr(
            $string,
            array(
                '.' => '+',
                '-' => '=',
                '~' => '/'
            )
    );

    return parent::decrypt($string);
}
}
?>