• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why $this->load->view() call the controller many times ??!!!

#1
[eluser]cmpsca[/eluser]
Hello all, today I faced a very weird problem in codeigniter. I inserted in my controller/test.php a code that will call a function increment_view() which will increment number of views of a page by 1 in its corresponding field in the database. I've noticed that whenever I refresh the page, logically the number of view of the page should increment by 1 in the database, but in reality it's being increment by 3 and for some other pages it's incremented by 5. Did anybody faced similar problem ?
I tried to comment blocks of codes in the controller and found that the $this->load->view() responsible of display part is actually causing this problem. When I comment it, the number is incremented by 1, if not commented the number of views is incremented by 3 or 5. Any idea of what is happening and how to solve it ?
Thank you Smile

#2
[eluser]cmpsca[/eluser]
I just caught the problem, yes it's the weirdest thing you will ever hear ... the problem is because of some images with wrong path ... I guess what is happening is that rather than loading the image, the link points to a 404 page which in its turn increment the view by 1. But why would wrong path of image execute the page of a 404 page instead of display the alt ...

#3
[eluser]CroNiX[/eluser]
Isn't a request for an image, or ANY resource that doesn't exist on the server, a 404 error? It should still show the alt text in its place, but it is a 404 error for that requested resource, not the entire page.

The same thing would happen if you called
www.yoursite/images/missing_image.jpg

It should cause a 404.

Where are you incrementing the view count? I'm sure you could move it somewhere where it would only get triggered once per page.

#4
[eluser]cmpsca[/eluser]
yeah missing resources are 404 but my custom 404 page also increment the view by one. But yes you're right about the 404 must affect the image only. The incrementation is done in the controller file:

class test extends CI_controller{
...
$this->test_model->increment();

$this->load->view('testView');
....
}

testView.php:
<img src="images/notFound.jpg" alt="not found">
etc..


The page displays correctly except for the image which is not found. But why the code for the page not found is executed when it reaches a broken reference. I didn't know that this could be a problem. But now I know Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.