Welcome Guest, Not a member yet? Register   Sign In
Controller created on image not found


Today I found a "bug" in my application that took me a couple of hours to figure out. I finally found the source of the problem, but it has confused me more than anything else.

So, to situate the context, in my application I want to do some actions to the user's session everytime the page is reloaded. To do that, on the constructor of the session object, I check if it's an Ajax request (with the input->is_ajax_request() function) and if that's the case, I skip the pertinent actions. Now, that seemed to work fine until the bug I mentioned appeared. Turns out that when a resource is missing from the page (an image in my case), codeigniter calls the 404 error page. I'm not even sure where or when, because it's not an Ajax request and the error page is not displayed anywhere.

I know all this can sound confusing, but I don't understand what is really going on. To test the problem, this are the changes I've made (to a clean CI 2.1.0 version). The bug doesn't appear if any of those is missing:

- Add a controller to override the 404 error page, I added this one:
// add application/controllers/Errors.php
Class Errors extends CI_Controller {

public function error_404() {
  echo 'error';


// on routes.php
$route['404_override'] = 'Errors/error_404';

- Use a controller that isn't the default one with an unexisting image, I used this:
// add application/controllers/Foo.php
Class Foo extends CI_Controller {

public function index() {
  echo '<img src="doesntexist.png" />';


- I couldn't figure out another way of debugging it, so I created a log to write the events:
// add on CodeIgniter.php line 356
$path = 'log.txt'; //Place log where you can find it
$file = fopen($path, 'a');
fwrite($file, "Calling method {$class}/{$method} with request {$_SERVER['REQUEST_URI']}\r\n");

And that's all, the results on the log when visiting (in my case) http://localhost/Test/index.php/Foo are:

Quote:Calling method Foo/index with request /test/index.php/Foo
Calling method Errors/error_404 with request /test/index.php/doesntexist.png

So, I don't know if this is a bug, but for me it's tottally unexpected that a controller is created when an image is not found.

Try this .htaccess
RewriteCond $1 !^(index\.php|assets|css|js|images|img|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L]

[quote author="InsiteFX" date="1329434055"]Try this .htaccess
RewriteCond $1 !^(index\.php|assets|css|js|images|img|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L]

That gives me the same output, I still can't figure out what's happening.

Theme © iAndrew 2016 - Forum software by © MyBB