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.

You log messages to somewhere. The controller parent class does the same in its __construct function, so in your error log you get two error messages that say "My controller initialised" and two error messages that say "Controller Class Initialized" - yes?
–
PickettSep 16 '11 at 15:18

Yes, Calle, but those aren't the only things that are being loaded twice, everything is being loaded twice and sent to the browser. I upaded my original post with what my log looks like when it loads twice.
–
aberrantSep 16 '11 at 15:36

3 Answers
3

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...
}

Peter's answer gets you close, but if you ever redirect from a url to the same url it will halt and result in an empty page. There is also an issue with using the back button in the browser. Can we handle this better?