2011-09-16 51 views
5

我遇到了CodeIgniter控制器被調用兩次的問題。它似乎只發生在我在uri中使用參數時(/ newsletter/confirm/a1938cas893vf9384f0384f0943)。如果我從我的函數中刪除參數,它只會加載控制器一次。我還注意到,使用url中的參數,如果我刷新頁面,它只加載一次。所以它似乎只在調用一個新頁面時加載兩次。CodeIgniter控制器在URL中使用參數時加載兩次

例如,第一次導航到/ newsletter/confirm/a123會導致它加載兩次。但如果你刷新/通訊/確認/ a123它只會加載一次。我完成了對我的觀點的註釋,以消除導致它的視圖的問題。

這聽起來像緩存問題,或者在我的.htaccess文件中?感謝您的任何建議。

相關負責人:

<?php 
error_reporting(-1); 
    ini_set('display_errors',1); 
class Test extends CI_Controller { 

    function __construct() { 
    parent::__construct(); 
     log_message('debug', 'MyController initialised'); 
    } 

    function confirm($code) 
    { 
     $this->load->helper(array('form')); 

     //$code = "6e930fe882c3b15712158812769dbcb636f96b8c"; 
     $result = $this->db->get_where('newsletter_members', array('nm_confirmation_code' => $code, 'nm_subscribed' => 0)); 

     if ($result->num_rows == 0) 
     { 
      $newsletter_message['newsletter_message'] = "Confirmation code is invalid or has already been confirmed."; 
      //$this->load->view('index_test', $newsletter_message); 
     } else { 
      $newsletter_message['newsletter_message'] = "Thank you for confirming your intent to subscribe to our newsletter!"; 
      $data = array(
        'nm_subscribed' => 1, 
        ); 
      $this->db->where('nm_confirmation_code', $code); 
      $this->db->update('newsletter_members', $data); 
      //$this->load->view('index_test', $newsletter_message); 
     } 

    } 

} 

?> 

.htaccess文件:

RewriteEngine On 
RewriteCond $1 !^([^\..]+\.php|robot\.txt|public|images|css|js|paul|event_docs|blog|citeforme|robots\.txt) 
RewriteRule ^(.*)$ /index.php/$1 [L] 

# BEGIN WordPress 
#<IfModule mod_rewrite.c> 
#RewriteEngine On 
#RewriteBase/
#RewriteCond %{REQUEST_FILENAME} !-f 
#RewriteCond %{REQUEST_FILENAME} !-d 
#RewriteRule . /index.php [L] 
#</IfModule> 
#RewriteEngine Off 
# END WordPress 

下面是日誌文件的樣子,你可以看到一切都被重新加載兩次:

DEBUG - 2011-09-16 09:59:34 --> Config Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> Hooks Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> Utf8 Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> UTF-8 Support Enabled 
DEBUG - 2011-09-16 09:59:34 --> URI Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> Router Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> Output Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> Input Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> Global POST and COOKIE data sanitized 
DEBUG - 2011-09-16 09:59:34 --> Language Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> Loader Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> Database Driver Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> Controller Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> MyController initialised 
DEBUG - 2011-09-16 09:59:34 --> Helper loaded: form_helper 
DEBUG - 2011-09-16 09:59:34 --> Final output sent to browser 
DEBUG - 2011-09-16 09:59:34 --> Total execution time: 0.0223 
DEBUG - 2011-09-16 09:59:34 --> Config Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> Hooks Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> Utf8 Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> UTF-8 Support Enabled 
DEBUG - 2011-09-16 09:59:34 --> URI Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> Router Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> Output Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> Input Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> Global POST and COOKIE data sanitized 
DEBUG - 2011-09-16 09:59:34 --> Language Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> Loader Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> Database Driver Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> Controller Class Initialized 
DEBUG - 2011-09-16 09:59:34 --> MyController initialised 
DEBUG - 2011-09-16 09:59:34 --> Helper loaded: form_helper 
DEBUG - 2011-09-16 09:59:34 --> Final output sent to browser 
DEBUG - 2011-09-16 09:59:34 --> Total execution time: 0.0213 
+0

您將消息記錄到某處。控制器父類在其__construct函數中的作用相同,因此在錯誤日誌中,您會看到兩條說明「我的控制器初始化」的錯誤消息和兩條說明「控制器類已初始化」的錯誤消息 - 是的? –

+0

是的,Calle,但這些不是唯一被加載兩次的東西,所有內容都被加載兩次併發送到瀏覽器。當我加載兩次時,我的日誌看起來像我的原始帖子。 – aberrant

回答

1

我不知道它是不是你的.htaccess文件,但我已經使用了一段時間,從來沒有問題:

RewriteEngine On 
RewriteBase/

# Allow any files or directories that exist to be displayed directly 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?/$1 [L,QSA] 

我想說的只是改變了文件中的第一,看看是否能解決問題,還請確保您的config.php文件的index_page變量爲空這樣的:

$config['index_page'] = ''; 

另一件事,你是否在你的routes.php文件中定義了任何路線?也許他們正在造成一些奇怪的循環,加載頁面兩次。

8

通常,這是由「髒」模板造成的虛假CSS,Javascript和圖像調用造成的。

最好嘗試通過仔細檢查模板中的所有資產調用來防止這種情況的發生,但是如果其他人正在執行有時不是選項的模板。

這是我在這種情況下所做的:

檢查是否HTTP_REFERRER是一樣的REQUEST_IRI。如果是這樣,你知道它是從當前加載的同一頁面調用的,因此你有一個虛假的資產調用。

我把下面的代碼放在頂部if控制器(這個代碼也可以在index.php入口點文件中工作)。

$self_referrer = $_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; 

    if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] == $self_referrer){ 
     return; // no point in going further since this is a bogus call... 
    } 
0

彼得的答案讓你關閉,但如果你曾經從一個網址重定向到相同的網址,它會停下來,並導致一個空的頁面。在瀏覽器中使用後退按鈕也有問題。我們可以處理這個更好嗎?

相關問題