title: Yii2: How to read a cookie, which was set by JavaScript
body: <p>I set a cookie in web/js/site.js:</p>
  
  <pre><code>$.cookie("sidebar", "hidden", { path: '/' });
  </code></pre>
  
  <p>I read the cookie with PHP:</p>
  
  <pre><code>$sidebar_toggle_state = $_COOKIE['sidebar'];
  </code></pre>
  
  <p>Is it possible to use also the Yii Request Component to read such cookies? The following code doesn't read the cookie:</p>
  
  <pre><code>$sidebar_toggle_state = Yii::$app-&gt;request()-&gt;cookies()-&gt;getValue('sidebar', '');
  </code></pre>
  
  <p>See: <a href="http://www.yiiframework.com/doc-2.0/guide-runtime-sessions-cookies.html#cookies" rel="noreferrer">http://www.yiiframework.com/doc-2.0/guide-runtime-sessions-cookies.html#cookies</a></p>
  
  <p>When I set a new cookie through the Yii Response Component:</p>
  
  <pre><code>Yii::$app-&gt;response-&gt;cookies-&gt;add(new yii\web\Cookie([
      'name' =&gt; 'sidebar',
      'value' =&gt; 'hidden',
  ]));
  </code></pre>
  
  <p>Then the value of the cookie looks like this (because the cookie is protected):</p>
  
  <pre><code>3976220a3c8e46bb641aef1da3accfb1652bffd5bb9de503a9d6882e8a69f6f9a%3A2%3A%7Bi%3A0%3Bs%3A7%3A%22sidebar%22%3Bi%3A1%3Bs%3A6%3A%22hidden%22%3B%7D
  </code></pre>
  
  <p>Such cookie can the Yii Request Component read. But can it read also unprotected cookies?</p>
  
  <p>Or, is it possible to set the Yii protected cookies with JavasSript / jQuery (without using AJAX)?</p>