CodeIgniter Forums
Header Response 200 and Content Type text/plain via Controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Header Response 200 and Content Type text/plain via Controller (/showthread.php?tid=23826)

Pages: 1 2 3


Header Response 200 and Content Type text/plain via Controller - El Forum - 10-30-2009

[eluser]shiftpoint[/eluser]
I actually found a work around. In the very first line of your controller add this:

<?php header("Content-Type: text/plain charset=UTF-8\r\n");

then you can use all of the CI built in stuff. That worked for me.


Header Response 200 and Content Type text/plain via Controller - El Forum - 11-13-2009

[eluser]Unknown[/eluser]
Quote:I actually found a work around. In the very first line of your controller add this:

<?php header(“Content-Type: text/plain charset=UTF-8\r\n”);

then you can use all of the CI built in stuff. That worked for me.

Looks like you need a ; after text/plain. I think if this isn't there, it doesn't replace what's already been sent as a header, and the output class will also send a header.

Correct:

Quote:<?php header(“Content-Type: text/plain; charset=UTF-8\r\n”); ?>



Header Response 200 and Content Type text/plain via Controller - El Forum - 01-10-2010

[eluser]Dinesh Shah[/eluser]
I hit this CI bug. The funny part was that it was working OK in my development server but was broken on my production server.

The issue is CI output class sends a spurious new line ("\n") before sending any custom headers.

Non-working code using CI's output class
Code:
$this->output->set_header("HTTP/1.0 200 OK");
        $this->output->set_header("HTTP/1.1 200 OK");
        $this->output->set_header('Expires: 0');
        $this->output->set_header('Cache-control: private');
        $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
        $this->output->set_header("Cache-Control: post-check=0, pre-check=0");
        $this->output->set_header("Pragma: no-cache");
        $this->output->set_header('Content-Description: File Transfer');
        $this->output->set_header("Content-type: text/csv");
//        $this->output->set_header("Content-type: application/csv");
        $this->output->set_header("Content-Disposition: attachment; filename=\"Client_Net_Balance-" . date('Y-m-d') . ".csv\"");
        echo $csv_file;

Working code using PHP's native header function. This works without any problem on development and production servers.

Code:
header("HTTP/1.0 200 OK");
        header("HTTP/1.1 200 OK");
        header('Expires: 0');
        header('Cache-control: private');
        header("Cache-Control: no-store, no-cache, must-revalidate");
        header("Cache-Control: post-check=0, pre-check=0");
        header("Pragma: no-cache");
        header('Content-Description: File Transfer');
        header("Content-type: text/csv");
//        header("Content-type: application/csv");
        header("Content-Disposition: attachment; filename=\"Client_Net_Balance-" . date('Y-m-d') . ".csv\"");
        
        echo $csv_file;



Header Response 200 and Content Type text/plain via Controller - El Forum - 02-16-2012

[eluser]Unknown[/eluser]
I know the post is old, but I had a similar problem when trying to minify css files into one, and I needed to add expires and content-type header. set_output worked like a charm! (CI 2.1)