php 如何在codeigniter中编码/解码url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29212131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:18:53  来源:igfitidea点击:

How to encode/decode url in codeigniter

phpcodeigniter

提问by Sumon

I am sending a link to my user's email to activate his account by click that link. example link: http://test.com/welcome/make_active_user/($user_id)

我正在发送一个链接到我的用户的电子邮件以通过单击该链接激活他的帐户。示例链接:http: //test.com/welcome/make_active_user/($user_id)

then I encode $user_idin my controller, after that my link looks like the following one

然后我$user_id在我的控制器中编码,之后我的链接看起来像下面的一个

http://test.com/welcome/make_active_user/17elQOjxrOXDsZdDZVFY7JFrB0TJFojdS+5OTpiIq/XXIaVrDfVWQ7xgOXXqGsURIFe/Udvm/XHrNWtbZXFA2g==

http://test.com/welcome/make_active_user/17elQOjxrOXDsZdDZVFY7JFrB0TJFojdS+5OTpiIq/XXIaVrDfVWQ7xgOXXqGsURIFe/Udvm/XHrNWtbZXFA2g==

upto this everything is fine. Now i want to decode the $user_id, but "/" symbol create problem. codeigniter took only those characters before the "/" symbol. How can i get a output of encoded user_id without "/"

到这一切都很好。现在我想解码$user_id,但是“ /”符号创建问题。codeigniter 只使用 " /" 符号之前的那些字符。如何在没有“ /”的情况下获得编码的 user_id 的输出

I use "$this->encrypt->encode($user_id,$key);" in my controller

$this->encrypt->encode($user_id,$key);在控制器中使用“ ”

Please help

请帮忙

回答by Philip

You need to modify the encrypt class to encode url safe strings.

您需要修改 encrypt 类以对 url 安全字符串进行编码。

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

Then you can create a $url_safelink

然后你可以创建一个$url_safe链接

Grab the encryption key from your config

从您的配置中获取加密密钥

$key = $this->config->item('encryption_key');

Create a url safe string by passing true as the third parameter

通过传递 true 作为第三个参数来创建一个 url 安全字符串

$encoded_url_safe_string = urlencode($this->encrypt->encode('secret', $key, true));

You will need to use rawurldecodeto decode the string

您将需要使用rawurldecode来解码字符串

$decoded_url_safe_string = rawurldecode($encoded_url_safe_string, $key);

回答by MDeuerlein

Just pass your encoded $user_idto the php function urlencode()before sending in a link like this:

在发送这样的链接之前,只需将您的编码传递$user_id给 php 函数urlencode()

$user_id_link = urlencode($user_id);

And decode the string you get back from the link with url decode()like this:

并解码您从链接中返回的字符串,url decode()如下所示:

$user_id = urldecode($user_id_link);

回答by Masoud Mustamandi

I faced the same problem in the past, codeigniter encrypt->encode()has disallowed chars for URL but i did the following to solve the problem

我过去遇到过同样的问题,codeigniterencrypt->encode()不允许使用字符作为 URL,但我做了以下操作来解决问题

  1. first encode()your $user_id
  2. simply replace the disallowed chars from the encrypted string with allowed one.
  3. now before decoding replace those chars back to original one
  4. decode()your $user_id
  1. 首先encode()你的$user_id
  2. 只需将加密字符串中不允许的字符替换为允许的字符即可。
  3. 现在在解码之前将这些字符替换回原始字符
  4. decode()您的 $user_id

here is the code:

这是代码:

$this->encrypt->encode($user_id,$key);
$user_id = strtr($user_id,array('+' => '.', '=' => '-', '/' => '~'));
//your email body (link the user id here)

now time for decoding

现在是解码的时间

$user_id = strtr($user_id,array('.' => '+', '-' => '=', '~' => '/'));
$this->encrypt->decode($user_id,$key);

回答by Miguel Angel Ivars Mas

I think your problem isn't with the '/', probably it's with '=' symbol.

我认为您的问题不在于“/”,可能与“=”符号有关。

To code and decode a url in CodeIgniter you can use any of this php native functions: http://php.net/manual/en/ref.url.php

要在 CodeIgniter 中对 url 进行编码和解码,您可以使用以下任何 php 本机函数:http: //php.net/manual/en/ref.url.php

But to avoid problems when decode the encoded url in CodeIgniter, you can add the special character that you require to use in each case (like '=' in your case) to $config['permitted_uri_chars'] in file application/config/config.php

但是为了避免在 CodeIgniter 中解码编码的 url 时出现问题,您可以将在每种情况下需要使用的特殊字符(例如 '=' 在您的情况下)添加到文件 application/config/config 中的 $config['permitted_uri_chars'] .php

回答by Satyam Sundaram

Try This:

尝试这个:

$query = urldecode($query);

$query = urldecode($query);

$query = urlencode($query);

$query = urlencode($query);