【问题标题】:How to pass safely text with URLs as a parameter to a CodeIgniter controller?如何安全地将带有 URL 的文本作为参数传递给 CodeIgniter 控制器?
【发布时间】:2014-08-20 05:21:04
【问题描述】:

我正在尝试使用 jQuery,通过 AJAX 从 JavaScript 发送一个可能包含一个或多个 URL 的字符串。

CodeIgniter 控制器将接收该文本。

我遇到了一些错误。有时是 404 错误,有时是 406 错误,具体取决于我发送数据的方式。

现在我这样发送:

var dsPost = encodeURIComponent(base64_encode(postContent));

$.ajax({
    url: "/posts/createTextPost/" + dsPost,
    type: "POST",
    data: "userIdWall" + "=" + userIdWall,
    success: function(data, textStatus, jqXHR) {

    },
    error: function (jqXHR, textStatus, errorThrown) {

    }
});

base64_encode fn 是 phpjs 的实现。

在 CI 控制器中我这样做:

public function createTextPost($dsPost) {
    $dsPost = base64_decode(urldecode($dsPost));
}

问题是,数据可以保存到数据库,但我不明白为什么会出现 404 错误。

有什么想法吗?

谢谢。

【问题讨论】:

    标签: javascript jquery codeigniter


    【解决方案1】:

    base64_encode() 函数有时会在编码字符串中添加/,因此Codeigniter 操作方法的行为类似于second 参数

    如果编码字符串如:qwqw221@1223/dds*(ewfd)

    比ci的问题

    "/" 字符后的字符串部分的行为类似于 2nd 参数

    '*' 是不允许的字符

    所以你应该使用GET查询字符串而不是CI参数

    喜欢

    url: "/posts/createTextPost/?crypt=" + dsPost
    

    并在CI 控制器操作中获取查询字符串

    public function createTextPost() {
         $dsPost = base64_decode(urldecode($this->input->get("crypt")));
    }
    

    【讨论】:

      【解决方案2】:

      我建议在 AJAX 调用中将 dsPost 添加到您的数据中,而不是将其作为参数添加到 url:

      $.ajax({
          url: "/posts/createTextPost/",
          type: "POST",
          data: { "dsPost": dsPost, "userIdWall", userIdWall },
          success: function(data, textStatus, jqXHR) {
            // Yippie!
          },
          error: function (jqXHR, textStatus, errorThrown) {
            // Bhuhuhu....
          }
      });
      

      ...然后更新您的 CI 控制器以使用发布的对象而不是输入参数:

      public function createTextPost() {
        $dsPost = base64_decode( urldecode( $this->input->post('dsPost') ) );
        userIdWall = $this->input->post('userIdWall');
      }
      

      【讨论】:

      • 嘿嘿!我是这样做的,但后来出现 406 错误。有任何想法吗?谢谢!
      • 我会试一试告诉你,谢谢你的建议!
      • 顺便说一句...根据您提供的示例脚本,您似乎正在将 AJAX 发布到同一个域,对吧?如果您将数据发布到另一个域,您将遇到“来源不允许”的问题。如果是这种情况,您最好使用 GET 方法而不是 POST。
      • 实际上不是,同一个域,但似乎问题在于服务器(在这种情况下由 godaddy 托管)有一些 apache 限制,当您通过 get 发送或发送到控制器时会触发 406 错误具有 http:// 或 https:// 的东西,因此解决方案是使用字符串替换函数来替换“正常”字符串的所有 http:// 和所有 https:// 出现,并将其替换回服务器端。
      【解决方案3】:

      您必须通过如下所示的 ajax 数据发送内容。

      $.ajax({
      //double check your url setting in this way in case you have diffrent setting for index.php      then remove the index.php
      url: "<?=base_url()?>.index.php/posts/createTextPost/",
      
      type: "POST",
      data: { 
             "dsPost": dsPost, 
             "userIdWall", userIdWall 
      },
      success: function(data, textStatus, jqXHR) {
      
      },
      error: function (jqXHR, textStatus, errorThrown) {
      
      }
      });
      

      然后在你的控制器中

      public function createTextPost() {
      $dsPost = base64_decode( urldecode( $this->input->post('dsPost') ) );
      $userIdWall=$this->input->post('userIdWall');
      

      我希望它会有所帮助。

      【讨论】:

      • 你好!我是这样做的,但我收到 406 错误。不知道是什么原因造成的。有任何想法吗?谢谢!
      • 请检查您的 url 设置 url: window.location.origin+'/posts/createTextPost',
      【解决方案4】:

      所以,感谢您的想法,但可以在这里找到答案:

      PHP/Apache Error:406 Not Acceptable

      我引用那里给出的答案:

      如果任何用户输入项正在启动,您的网站就会产生错误 使用 http:// 或 https:// 。

      当我尝试使用以 http:// 开头的链接时,我得到了 406 Not 可以接受:

      http://onkore.us/?blah=http://www.google.com

      我试试这个没问题:

      http://onkore.us/?blah=www.google.com

      【讨论】:

        猜你喜欢
        • 2023-04-09
        • 2011-08-17
        • 1970-01-01
        • 2011-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-04
        • 1970-01-01
        相关资源
        最近更新 更多