LoginSignup
0
1

More than 5 years have passed since last update.

Symfony2でCookieにJSONを書いたり読んだりする

Last updated at Posted at 2015-06-01

書く

$name  = 'hoge';
$value = array('foo', 'bar');
$response->headers->setCookie(new Cookie($name, json_encode($value)));

読む

$name    = 'hoge';
$cookies = $this->container->get('request')->cookies;
$value   = json_decode($cookies->get($name), true);

余談

端末毎のCookieサイズ上限であるとか、そもそもCookieに対応してない端末の存在とか、secure属性を付けるべきか否かとか、セッションを使うべきではないかとか、ユーザが送信してくるデータなので信用せず毎回バリデーションしなければならないとか、仮に4KBのcookieを付けると設定したパスの全てのリクエストで4KBのcookieを受信する事になるから静的な画像とかcssとかjsとかはクッキーレスの別ドメインから配信した方がいいんじゃないとか、色々あるけど割愛。

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1