CodeIgniter Forums
All my controllers are being called twice. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: All my controllers are being called twice. (/thread-33203.html)



All my controllers are being called twice. - El Forum - 08-18-2010

[eluser]mrbinky3000[/eluser]
It is entirely possible that I've made a huge mistake someplace, but for some reason, my controllers are being called twice. This is breaking DX_Auth's captcha's on Chrome, but somehow, Firefox and IE can handle it.

The problem is, every controller is getting called twice milliseconds apart. I used log_message() to print every key/value pair from the $_SERVER superglobal.

The only differences are:

1st Call
Code:
'HTTP_CACHE_CONTROL' => 'max-age=0'
'HTTP_ACCEPT' => 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
'HTTP_COOKIE' => 'ci_session=a:4:{s:10:"session_id";s:32:"4be9cef4e2cdae468c7443f52a5fb3f2";s:10:"ip_address";s:13:"321.321.321.321";s:10:"user_agent";s:50:"Mozilla/5.0+(Windows;+U;+Windows+NT+6.1;+en-US)+Ap";s:13:"last_activity";s:10:"1282151409";}70b300096c8d40c60a676ac65bcb222c'
'REMOTE_PORT' => '61828'

and the 2nd Call

Code:
'HTTP_CACHE_CONTROL' => NULL
'HTTP_ACCEPT' => '*/*'
'HTTP_COOKIE' => 'ci_session=a:4:{s:10:"session_id";s:32:"a5bb2c98a8ff4438cef3a3fe3d5ff73e";s:10:"ip_address";s:13:"321.321.321.321";s:10:"user_agent";s:50:"Mozilla/5.0+(Windows;+U;+Windows+NT+6.1;+en-US)+Ap";s:13:"last_activity";s:10:"1282151734";}4adf1de21f9708d66b3d4bc36d0b0d92'
'REMOTE_PORT' => '61842'

There is a thread already on the CI forums where someone else had a similar problem, but his problem turned out to be spyware. This is not the case for me. I tried on several computers and none of my visitors can register for the site.

You see, DX_auth stores the value of the captcha as CI session flashdata on the first call. The second call wipes the flashdata and no one can ever get the captcha correct as a result.

Here is a sample of my access logs

Code:
123.123.123.123 - - [18/Aug/2010:12:31:26 -0500] "GET /welcome HTTP/1.1" 200 3391 "http://somewhere.com/[age" "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.126 Safari/533.4"
123.123.123.123 - - [18/Aug/2010:12:31:27 -0500] "GET /welcome HTTP/1.1" 200 3391 "-" "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.126 Safari/533.4"

As you can see, the first call seems to be coming from me, and the second call has no referrer.

I thought there was a problem with the .htaccess file I used to eliminate index.php from the uri. So, I switched to the "default" .htaccess redirect as stated in the CI documentation. The double load still happens.

Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|asset|captcha|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]

I also used FireFox's "Live HTTP headers" plugin. Only one request is being sent by the browser and I believe it only receives only one reply.

Anybody know what's going on? Has this happened to anyone else?


All my controllers are being called twice. - El Forum - 08-18-2010

[eluser]WanWizard[/eluser]
This can happen if there is another element on the page (p.e. an image) that gets rewritten to index.php.

Use firebug's 'net' option to see what requests go to your server after you've requested the page, and see if there's something out of the ordinary (like an image URL not starting with assets or captcha). You could also enable rewrite logging (requires access to httpd.conf) to check the rewrite requests.


All my controllers are being called twice. - El Forum - 08-18-2010

[eluser]mddd[/eluser]
You're saying it only causes problems in Chrome. Is the double calling also happening in other browsers or just in Chrome?
I agree with WanWizard, it might well be an image or something like that. Specifically, might it be that you have not stopped url rewriting for favicon.ico?
It seems (from what I've read) that other browsers 'remember' better when there is no favicon.ico, while Chrome tries to find it every time.


All my controllers are being called twice. - El Forum - 08-18-2010

[eluser]mrbinky3000[/eluser]
The double calling is happening on all browsers. Chrome seems more "sensitive" to it for some reason. IE and FireFox will let you register on your second registration attempt for some reason.


All my controllers are being called twice. - El Forum - 08-18-2010

[eluser]mrbinky3000[/eluser]
[quote author="mddd" date="1282179072"]You're saying it only causes problems in Chrome. Is the double calling also happening in other browsers or just in Chrome?
I agree with WanWizard, it might well be an image or something like that. Specifically, might it be that you have not stopped url rewriting for favicon.ico?
It seems (from what I've read) that other browsers 'remember' better when there is no favicon.ico, while Chrome tries to find it every time.[/quote]

There was another post somewhere in the forum where the person placed a favicon.ico file in their folder and it fixed his problem. I do have a valid favicon.ico file in the web root.

However, I thought I already WAS stopping url rewriting for favicon.ico.

Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|asset|captcha|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]

Doesn't line two say "if it doesn't begin with index.php, asset, captcha, robots.txt or favicon.ico then redirect it to index.php" ?


All my controllers are being called twice. - El Forum - 08-18-2010

[eluser]WanWizard[/eluser]
Have you followed my advice and checked in firebug which files are requested from your server when you request the page?

Because if this is the problem, one of them is the culprit, and looking at them is probably a lot quicker that guessing, especially at our end of the internet... :-P


All my controllers are being called twice. - El Forum - 08-18-2010

[eluser]mrbinky3000[/eluser]
I checked firebug and didn't see a problem. Firebug only shows you the result of requested files.
I enabled Rewrite Logging and the feedback was a little too complicated to decipher.

I ended up solving the problem!

What I did was create a new, bare-bones template and proceeded to add new elements one at a time until the problem showed its head.

This was the line causing the problem....

Code:
<link rel="shortcut icon" href="" />

It seems "" was being interpreted by the Rewrite rules in such a way that caused a second call.

To fix the problem, I changed the line to..

Code:
<link rel="shortcut icon" href="/favicon.ico" />

Is there some sort of rewrite condition that we can add to the rewrite example shown in the CI documentation that will handle null or blank requests?

Anyone know what lines I would need to add to my rules to catch this condition and ignore it?


All my controllers are being called twice. - El Forum - 08-18-2010

[eluser]mddd[/eluser]
Just don't do a blank request from your code! Problem solved.


All my controllers are being called twice. - El Forum - 08-19-2010

[eluser]WanWizard[/eluser]
An empty link href just requests the current URL ( it's interpreted as a relative link ), so nothing you can do in your rewrite rules to prevent it, mod_rewrite sees it as just another request.