CodeIgniter Forums
how do I read request headers in codeigniter - 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: how do I read request headers in codeigniter (/showthread.php?tid=38813)



how do I read request headers in codeigniter - El Forum - 02-19-2011

[eluser]Unknown[/eluser]
I tried using this
Code:
$this->input->get_request_header()
; to get the cookies being sent by the header in my controller, but I keep getting this message

Call to undefined method
Code:
CI_Input::get_request_header()
.

Any idea why? I need to read a cookie value on the page and looks like I would have to do that with the headers being sent in.


how do I read request headers in codeigniter - El Forum - 02-19-2011

[eluser]Basketcasesoftware[/eluser]
There are already cookie functions in CI.
There are the following methods available in the input class
$this->input->cookie() // Returns the selected cookie value.
$this->input->get_cookie() // Gets a specific cookie.
$this->input->set_cookie() // Reverse of the previous.


I don't know why are getting that error message. What version of CI are you using?
Are you using it in a library or a controller method?


how do I read request headers in codeigniter - El Forum - 02-19-2011

[eluser]Unknown[/eluser]
I did try using the $this->input->get_cookie() and set_cookie() method ways you suggested above in my controller, but I got a similar error message.

I am using 2.0 and am calling it from my controller. Am I doing it wrong?


how do I read request headers in codeigniter - El Forum - 02-19-2011

[eluser]Basketcasesoftware[/eluser]
How about sharing the section of code you are calling it from. No one can help you without some idea of the context where your problem is occurring.


how do I read request headers in codeigniter - El Forum - 03-11-2011

[eluser]adgezaza[/eluser]
I'm having a similar issue.

I'm trying to make a call to the get_cookie() method from the input library but I am receiving this error.

"Fatal error: Call to undefined method CI_Input::get_cookie() "

I've loaded the helper for cookie and have used the call set_cookie() in the parent function. I'm not sure if this should be an issue.

Here is the code that is creating the error.

Code:
class Main extends MY_Controller
    {
        function __construct()
        {
            parent::__construct();

            //load library
            $this->load->helper('cookie');

            echo "Welcome controller<br>";

        }

        function index()
        {  


            //get platform cookie
            $_platform = $this->input->get_cookie('platform'); //<-ERROR

            if (!$this->tank_auth->is_logged_in()) {
                redirect('/auth/login/');
            } else {
                $data['user_id']    = $this->tank_auth->get_user_id();
                $data['username']   = $this->tank_auth->get_username();
                $data['platform']   = $_platform['value'];
                $this->load->view('welcome', $data);
            }
        }
    }



how do I read request headers in codeigniter - El Forum - 03-11-2011

[eluser]Edmundas KondraĊĦovas[/eluser]
Since you've loaded the cookie helper, you can just fetch the cookie with a function.

Code:
$_platform = get_cookie('platform');

And if I recall correctly, getting the cookie via Input class is like this:

Code:
$_platform = $this->input->cookie('platform');



how do I read request headers in codeigniter - El Forum - 03-11-2011

[eluser]adgezaza[/eluser]
You're right. And my 2nd day on CI is really shining through :/


how do I read request headers in codeigniter - El Forum - 03-11-2011

[eluser]bubbafoley[/eluser]
[quote author="Edmundas KondraĊĦovas" date="1299907417"]Since you've loaded the cookie helper, you can just fetch the cookie with a function.

Code:
$_platform = get_cookie('platform');

And if I recall correctly, getting the cookie via Input class is like this:

Code:
$_platform = $this->input->cookie('platform');
[/quote]

This is correct. There isn't a function in the Input class called get_cookie(). The user guide implies there is one but upon inspection of the code the function doesn't exist. It makes sense, though, because get_cookie() in the cookie helper uses $this->input->cookie().

Code:
function get_cookie($index = '', $xss_clean = FALSE)
{
    $CI =& get_instance();

    $prefix = '';

    if ( ! isset($_COOKIE[$index]) && config_item('cookie_prefix') != '')
    {
        $prefix = config_item('cookie_prefix');
    }

    return $CI->input->cookie($prefix.$index, $xss_clean);
}