CodeIgniter控制器在URL中使用参数时加载两次

时间:2011-09-16 15:08:34

标签: php codeigniter

我遇到的问题是我的CodeIgniter控制器被调用了两次。它似乎只发生在我使用uri中的参数时(/ newsletter / confirm / a1938cas893vf9384f0384f0943)。如果我从我的函数中删除参数,它只加载一次控制器。我还注意到,使用url中的参数,如果我刷新页面,它只加载一次。因此,只有在调用新页面时才会加载两次。

例如,第一次导航到/ newsletter / confirm / a123会导致加载两次。但如果您要刷新/ newsletter / confirm / 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

4 个答案:

答案 0 :(得分:8)

通常这是由“脏”模板造成虚假CSS,Javascript和图像调用造成的。

最好通过仔细检查模板中的所有资产调用来防止这种情况发生,但是如果其他人正在执行有时不是选项的模板。

以下是我在这种情况下所做的事情:

检查HTTP_REFERRER是否与REQUEST_IRI相同。如果是这样,你知道它是从当前加载的同一页面调用的东西,因此你会对丢失的资产进行虚假调用。

如果控制器(此代码也适用于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...
   }

答案 1 :(得分: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文件中定义了哪些路由?也许他们造成一些奇怪的循环,正在加载页面两次。

答案 2 :(得分:0)

彼得的回答让你接近,但如果你从一个网址重定向到同一个网址,它将停止并导致一个空页面。使用浏览器中的后退按钮也存在问题。我们可以更好地处理这个问题吗?

答案 3 :(得分:0)

我遇到了类似的问题(控制器加载了两次,但与参数无关),对我来说,问题是有人在加载了未缩小版本的js文件时已经加载了该文件。

删除缩小版本后,它开始正常工作。