CodeIgniter Forums
How do I fix this Header Problem ? - 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: How do I fix this Header Problem ? (/thread-52784.html)

Pages: 1 2


How do I fix this Header Problem ? - El Forum - 06-26-2012

[eluser]vincej[/eluser]
On my development server I do not get this problem, however, I have recently moved my site onto an ISP in a testing environment and now I am getting a nasty error:

Code:
Message: Cannot modify header information - headers already sent by (output started at /services9/webpages/g/r/my-site.com/public/CLFarms/system/core/Exceptions.php:185)

I have looked that the controller being called and indeed there are 3 redirects, but I do not understand why they don't give me an error locally but they do on my remote ISP testing server,

What is also relevant is that if you reload the page the errors go away and the page loads normally.

Can someone with more experience give me advice as to where I am going wrong ?

MANY MANY THANKS !

Controller

Code:
function cart($productid=0){
        
  if (!isset($_SESSION['userid'])){
  $this->session->set_flashdata('error', 'You will need to login to place an order' );
  redirect('welcome/index','refresh') ;}
  
if ($productid > 0){               // IF PRODUCT PRESENT THEN ADDS TO CART
            
  $productid = $this->uri->segment(3);
  $fullproduct = $this->MProducts->getProductUserCode($productid);
  
    
  $this->MOrders->updateCart($productid,$fullproduct);  // UPDATE CART ADDS ONE ITEM TO CART IF THERE IS NONE AND INCREMENTS COUNT +1 IF ITEM IS IN CART ALREADY.
  redirect('welcome/product/'.$productid, 'refresh');  
}
else{                 // GOES TO 'VIEW CART' AND SHOWS CONTENTS OF CART
  $data['title'] = "MY SITE | Shopping Cart";
  if (count($_SESSION['cart'])){
   $data['main'] = 'shoppingcart';
   $data['location'] = $this->MCustomer->GetCustomerPickupLocation(); // This delivers a full text version of the location not an ID //  
   $data['deliverydates'] = $this->MPickup->GetLocationDates($data['location']);
   $data['navlist'] = $this->MCats->getCategoriesNav();
   $this->load->vars($data);
   $this->load->view('template');
  }else{
   redirect('welcome/index','refresh');
  }
}
  }




How do I fix this Header Problem ? - El Forum - 06-27-2012

[eluser]vincej[/eluser]
HI Again - I should have mentioned in my earlier post that the code relating to Line 185 and causing the problem is below - It manages error reporting and output buffering and come from system/core /exceptions:

Code:
<?php

function show_php_error($severity, $message, $filepath, $line)
{
  $severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];

  $filepath = str_replace("\\", "/", $filepath);

  // For safety reasons we do not show the full file path
  if (FALSE !== strpos($filepath, '/'))
  {
   $x = explode('/', $filepath);
   $filepath = $x[count($x)-2].'/'.end($x);
  }

  if (ob_get_level() > $this->ob_level + 1)
  {
   ob_end_flush();
  }
  ob_start();
  include(APPPATH.'errors/error_php.php');
  $buffer = ob_get_contents();
  ob_end_clean();
  echo $buffer;                                                   // <==== LINE 185 HERE
?&gt; }



How do I fix this Header Problem ? - El Forum - 06-27-2012

[eluser]PhilTem[/eluser]
Do you have any white-spaces at the very top of your files? This might be one reason, since I don't think that there is an actual error on line 185 of Exceptions.php.

Oh, and by the way: You may want to initiate the $data array before populating it with data like so

Code:
else{                 // GOES TO 'VIEW CART' AND SHOWS CONTENTS OF CART
  
  $data = array();
  
  $data['title'] = "MY SITE | Shopping Cart";
  if (count($_SESSION['cart'])){
   $data['main'] = 'shoppingcart';
   $data['location'] = $this->MCustomer->GetCustomerPickupLocation(); // This delivers a full text version of the location not an ID //  
   $data['deliverydates'] = $this->MPickup->GetLocationDates($data['location']);
   $data['navlist'] = $this->MCats->getCategoriesNav();
   $this->load->vars($data);
   $this->load->view('template');
  }else{
   redirect('welcome/index','refresh');
  }
}



How do I fix this Header Problem ? - El Forum - 06-27-2012

[eluser]CroNiX[/eluser]
Well, your code is producing a PHP error somewhere else, which gets sent to the screen as output before your controller has finished running.

The actual error isn't 'in' Exceptions.php. The error is happening in your code and when the error gets triggered CI calls the Exceptions class to output it. It's just the symptom, not the cause.

This would be very quick to track down if you ran a debug_backtrace() on it so you can see what error is triggering the Exceptions class. If you have a stepping debugger, it would take about 3 minutes to step through that code and see where the error is getting triggered.


How do I fix this Header Problem ? - El Forum - 06-27-2012

[eluser]vincej[/eluser]
Thanks guys - I really need you help - I have to go live soon and I'm just not getting anywhere. I don't get the fact that on my dev. machine there is no error. Just on the ISP. Why is that ?

I have checked that there is no white space on either the beginning or end of the dependant files ie all the models, the redirects and the view.

CroxNix - you mention having a stepping debugger. I use dreamweaver which has no such tool. Can you suggest something easy to use ? I have tried Eclipse and it is in my opinion a pain. I have already spent hours trying to fix this.

Many thanks !


How do I fix this Header Problem ? - El Forum - 06-27-2012

[eluser]CroNiX[/eluser]
The only IDE I use is Zend Studio, which is built on top of Eclipse. There are others that have stepping debuggers, of course, but I can't really list them as I don't use them or want to waste time checking out other IDEs as I've been using this one for 6 years now.

As far as why there is a difference between your dev/live servers, most likely it is some config setting such as error reporting. Do you use the ENVIRONMENT settings in CI? Is your live server set to suppress outputting of errors in your php.ini as well as CI as well as having a more strict error reporting level set?


How do I fix this Header Problem ? - El Forum - 06-27-2012

[eluser]vincej[/eluser]
I use ENVIRONMENT settings - it is set to 'development', with error reporting E_ALL and yet my dev system does not generate an error. I can't get to the php.ini file on my ISP.

I will look at debug_backtrace() .. but I do nto understand how it will help me find the error.

CroxNix Many Many Thanks ! -- You are my only hope !



How do I fix this Header Problem ? - El Forum - 06-27-2012

[eluser]vincej[/eluser]
something Else which might be relevant: The controller is called when I select a product. I get the header error. However if I hit refresh the page loads without error - I don't get it, but it might be helpful.

Thanks !


How do I fix this Header Problem ? - El Forum - 06-27-2012

[eluser]CroNiX[/eluser]
It could be a session problem, like you use something that isn't defined the first time you visit but if you refresh it does.

It's a bit confusing that you are mixing $_SESSION and CI's $this->session as they are totally separate mechanisms.


How do I fix this Header Problem ? - El Forum - 06-27-2012

[eluser]vincej[/eluser]
My controller calls a model "MOrders". I have been getting an undefined offset error in MOrders. Could this be responsible for my header order ? I have so far believed not, hence I wanted to address the header error first.

Should I switch my focus to he MOrder problem first ?