Ignore Slash while using encryption in codeigniter

丶灬走出姿态 提交于 2019-12-08 04:50:25

问题


I have a problem to encrypt/decrypt the email, i just send a link on mail like this

http://www.domain.com/mycontroller/myfunction/McvBsce........etc

The last Segment is actually a encrypted email id, I decrypt this email and update stus in my db when user click on this link.All done right.

Problem: When url like this

http://www.domain.com/mycontroller/myfunction/McvB/sce

It shows 404 error, because slash included in the generated encryption key.How can i ignore the slash while it generate the encryption, that's my main problem, rest are working fine.


回答1:


You need to use this class, include this file in your applicatio/libraries folder, I had the same issue:

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

Credit goes to the codeigniter forum, from where I got this.



来源:https://stackoverflow.com/questions/22885048/ignore-slash-while-using-encryption-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!