0

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?

loki9
  • 625
  • 7
  • 20
  • maybe your `cookie` is above the maximum size? http://stackoverflow.com/questions/640938/what-is-the-maximum-size-of-a-web-browsers-cookies-key – tomexsans Sep 12 '14 at 06:41
  • ahh, maybe I am trying to store a long string of json in chat['value'].. any workaround you might suggest? – loki9 Sep 12 '14 at 06:49
  • Depends for what you need the cookies for, sessions might be a good replacement. – Patrick Sep 12 '14 at 10:01
  • It says that session and cookies are the same the only difference is sessions are stored at the server side and cookies are stored at the client side, is there any chance that sessions can carry larger amount of data than cookies? but would affect the load speed? – loki9 Sep 12 '14 at 15:12

1 Answers1

1

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

Patrick
  • 3,289
  • 2
  • 18
  • 31