簡體   English   中英

Codeigniter:如何處理錯誤URL中的不必要結果?

[英]codeigniter: how to handle unwanted result in error url ?

我是一名新的CI開發人員,在一個網站上處理安全性問題。

我有一個類似的網址:

http://myweburl/Everything/gov/2

其中“ 2”表示查詢數據庫的參數。

這是我的問題:如果有人瀏覽以下網址:

http://myweburl/Everything/gov/2/order.txt

網址中實際上沒有任何內容,但是..他會得到一個空的結果頁面。(回復200 OK)

這不是我想要的。 我希望他會收到404錯誤或其他提示,告訴用戶網址中沒有任何內容。

誰能給我一些線索來實現我的目標? 非常感謝!

基本上,您只想顯示有效的頁面(如果它們是表示查詢中參數的URL中正確的URI段數)。

在調用任何其他方法之前,您應該檢查URI segments的數量作為預處理。 這樣,您可以確定網址中實際上只有1個細分

如果您發現URL存在三個以上的段,則可以使用CodeIgniter提供的此類錯誤處理的便捷功能來相應地處理該情況

您可以檢查URL中正確的段數,如果不是您想要的段數,則調用show_404()方法。

在您發布的示例URL中: http://myweburl/Everything/gov/2/order.txt ,實際上有4個細分。

    // from the docs http://codeigniter.com/user_guide/libraries/uri.html

     $total_segments = $this->uri->total_segments();

    // 3 segments would be the max if this is your url:
    // http://myweburl/Everything/gov/2/order.txt (this url contains 4 segments)
    //                 ^controller/function/uri_segment
    // this means that there are 3 segments in your URL
    // you don't want to have more than 3, so check for that
    // with the total_segments() function

     if($total_segments > 3)

     {
           $valid = false;
           show_404();

     }

     else 

     {

          $valid = true;

     }

     if($valid) 

     {

     // process data as usual

     }

暫無
暫無

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

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