簡體   English   中英

如何安全地將帶有URL作為參數的文本傳遞給CodeIgniter控制器?

[英]How to pass safely text with URLs as a parameter to a CodeIgniter controller?

我試圖通過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錯誤。

有任何想法嗎?

謝謝。

base64_encode()函數有時添加/在編碼的字符串,因此Codeigniter動作方法表現得像second paramameter

如果編碼字符串像:qwqw221 @ 1223 / dds *(ewfd)

比ci問題

“ /”字符后的字符串部分,其行為類似於第二個參數

不允許使用“ *”字符

因此,您應該使用GET查詢字符串而不是CI參數

喜歡

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

並在CI控制器操作中獲取查詢字符串

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

我建議將dsPost添加到AJAX調用中的數據中,而不是將其作為參數添加到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');
}

您必須通過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');

希望對您有所幫助。

因此,感謝您的想法,但可以在這里找到答案:

PHP / Apache錯誤:406不可接受

我引用那里給出的答案:

如果任何用戶輸入項以http://或https://開頭,則您的網站將生成錯誤。

當我嘗試以http://開頭的鏈接時,出現了406不可接受:

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

當我嘗試這個很好

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM