Welcome Guest, Not a member yet? Register   Sign In
set_content_type() Doesn't seem to be working?
#1

Hey guys, im trying to set the header type on a page, heres the code, pretty basic:
PHP Code:
public function current()
    {
        
$this->output->set_content_type('text/plain');

        if( ! 
$partition $this->Accounts_model->some_function())
        {
            die(
"No Partition Set");
        }

        echo 
$partition;
    } 

It doesnt at all change the header, however if I change it to this:
PHP Code:
public function current()
    {
        
header("Content-Type:text/plain");

        if( ! 
$partition $this->Accounts_model->some_function())
        {
            die(
"No Partition Set");
        }

        echo 
$partition;
    } 

It works fine. I dug through the Output.php file, and notice on line 467, you disable the errors of the header() function by using a @, if I take that out, I get the error..
Quote:Message: Cannot modify header information - headers already sent by (<MY FILE:LINE>)

Filename: core/Output.php

Line Number: 467

So is there a reason the set_content_type() is executing after the page output?
Reply
#2

Well... you did the echo $partition inside a controller.... You should use $this->load->view('your_view'); and put that echo inside the view.
Reply
#3

(This post was last modified: 07-06-2015, 11:06 AM by CroNiX.)

Yes, you need to use a view, OR use set_output() because of the way CI buffers output. If you echo from a controller, that happens before anything else since the controller has to complete before CI will send IT's data out. And using output->set_content_type() is part of that, which is why it doesn't work the way you wrote it and using native PHP header() does.

A workaround is to finish what you started and send your output using output->set_output()
http://www.codeigniter.com/user_guide/li...set_output

$this->output->set_output($partition); //instead of echo $partition
Reply
#4

Oh, duh, sorry, lol
Reply




Theme © iAndrew 2016 - Forum software by © MyBB