In yii, you can quickly and easily create a cookie with a value. Example:

$cookie = new CHttpCookie('users',$model->id); Yii::app()->request->cookies['users']=$cookie; 

The question is: if I need to store, for example, in the user id cookie, then how to organize it correctly? Logic suggests that the value of a cookie should be something like this "12, 23, 44, 11, 123". If so, how to make such a string and bind it to the value of this cookie?

  • And why in the cookie store id users? - zb '
  • For example, if a user who has a cookie stored, it is necessary to issue a list of other users viewed by him in the event that he is not authorized. - makbeth
  • if he is not authorized, then why should he give something away? - zb '
  • A strange question that is not relevant to my task. I gave the user id for example. It can be anything: id photos, products, etc. - makbeth
  • Well, the answer is - do not store anything in the cookie, except for the session identifier (and this is not necessary), everything else is in the session itself or webdb / localstorage. - zb '

1 answer 1

The value of the cookie can be a string. To write an array or an object into a string, you must serialize it in one of the ways. For example, serialize or json_encode .

The limit on the length of one cookie: 4096 bytes, including the name and attributes.