How to use header in codeigniter?

Member

by lizzie , in category: PHP Frameworks , 7 months ago

How to use header in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by scotty_walker , 7 months ago

@lizzie 

In CodeIgniter, you can use the $this->output->set_header() method to set headers in your controller methods.


Here is an example of how you can use headers in CodeIgniter:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class YourController extends CI_Controller {

    public function index() {
        // Set a custom header
        $this->output->set_header('X-Custom-Header: Custom Value');

        // Send JSON content type header
        $this->output->set_header('Content-Type: application/json');

        // Load a view
        $this->load->view('your_view');
    }

}


In this example, we are setting a custom header X-Custom-Header: Custom Value and setting the Content-Type header to application/json. You can set any type of header using this method.


Make sure to set the headers before any output is sent to the browser.