Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter Controllers Loading Twice When Using Parameters in the URL

I'm having an issue where my CodeIgniter controllers are being called twice. It only seems to occur when I am using parameters in the uri (/newsletter/confirm/a1938cas893vf9384f0384f0943). If I remove the parameter from my function it only loads the controller once. I also noticed that with the parameter in the url, if I refresh the page it only loads once. So it seems that it is loading twice only when a new page is called.

For example navigating to /newsletter/confirm/a123 for the very first time will result in it loading twice. But if you were to refresh /newsletter/confirm/a123 it will only load once. I've completed commented out calls to my view to eliminate an issue with the view causing it.

Does this sound like a cache issue, or something in my .htaccess file? Thanks for any suggestions.

Relevant controller:

<?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 file:

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

Here's what the log file looks like, you can see everything is being reloaded twice:

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
like image 578
aberrant Avatar asked Sep 16 '11 15:09

aberrant


2 Answers

Generally this is caused by "dirty" templates making bogus CSS, Javascript and image calls.

It is best to try to prevent this from from happening by carefully inspecting all the assets calls in your templates but if someone else is doing the templates that is sometimes not an option.

Here's what I did in that case:

Checking if HTTP_REFERRER is the same as the REQUEST_IRI. If so you know it is something being called from the same page that is currently loaded, thus you have a bogus call to a missing asset.

I place the following code at the top if controller (this code also works in the index.php entry point file).

   $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...
   }
like image 147
Peter Drinnan Avatar answered Sep 21 '22 04:09

Peter Drinnan


I don't know if it's your .htaccess, file, but I've used this for a while and never had a issue:

 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]

I'd say just change the file first and see if that solves the issue, also, make sure that in your config.php file the index_page variable is blank like this:

 $config['index_page'] = '';

one other thing, are you defining any routes in your routes.php file? Maybe they are causing some strange loop that is loading the page twice.

like image 38
JoeCianflone Avatar answered Sep 17 '22 04:09

JoeCianflone