Search code examples
phpcodeignitercookiessetcookie

Code Igniter cannot insert more than one cookie


Im currently using Code igniter, and having problems with inserting 2 cookies at the same time. here is my code:

    $cookie = array(
        'name'   => '__ncookie',
        'value'  => json_encode(array("fr" => 0,    "dc"=> 0,"cor"=> 0 )),
        'expire' => '86500',
        'secure' => false
    );
    $this->input->set_cookie($cookie);
    $chat = array(
        'name'  => '__nsgs',
        'value' => json_encode($this->mchat->get_unread()),
        'expire' => '86500',
        'secure' => false
    );
    $this->input->set_cookie($chat);// when var_dump = NULL

WarningCannot modify header information - headers already sent by (output started at C:\xampp\htdocs\onyx_agent\application\controllers\clogin.php:59)core/Input.php286

Am I doing something wrong?


Solution

  • I was going to write this as a comment, But it was getting too long.

    Cookies are critical if need to pass information to a different website, for example tracking users where they visit , or where they came from(like tracking which ad-campaign a user got to your website from).

    Sessions on the other hand are used internally by your own server. This means that a session on website A normally can't be used on website B, If you're saving the cookie/session for usage in your own website and you have alot of information to store, You can use a database table to store the sessions and simply give the client a sessionID.

    So I go into your website, you assign a cookie/session ID of 42 to my user(at this point it doesn't matter much if it's sessions or cookies), When I send an http request with my browser to view the website, I pass you my cookie/session, You can then use the ID to run a SQL query and get all the juicy data for my user.

    However, If you need all this data to be used by a different website, You'll need to think of a different solution