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


CodeIgniter Controllers Loading Twice When Using Parameters in the URL

我有一个问题,我的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

通常这是由于"脏"模板进行虚假的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...
   }

我不知道这是不是你的。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文件中定义了任何路由?也许它们会导致一些奇怪的循环,加载页面两次。

Peter的答案让你接近了,但是如果你从一个url重定向到相同的url,它将停止并导致一个空页面。在浏览器中使用后退按钮也有一个问题。我们能处理得更好吗?

我有一个类似的问题(控制器加载两次,但与参数无关),对我来说,问题是有人加载了一个缩小的js文件,而非缩小的版本已经被加载。

删除迷你版本后,它开始正常工作