{"id":4978,"date":"2021-08-09T07:58:14","date_gmt":"2021-08-09T07:58:14","guid":{"rendered":"https:\/\/www.bearloga.space\/?page_id=4978"},"modified":"2021-08-09T07:58:15","modified_gmt":"2021-08-09T07:58:15","slug":"yii-ispolzovanie-cookies","status":"publish","type":"page","link":"https:\/\/www.bearloga.space\/uk\/yii-ispolzovanie-cookies\/","title":{"rendered":"Yii \u2014 \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 Cookies"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u0424\u0430\u0439\u043b\u044b cookie \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u0445.&nbsp;\u0412 PHP \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a \u043d\u0438\u043c \u0434\u043e\u0441\u0442\u0443\u043f \u0447\u0435\u0440\u0435\u0437 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e&nbsp;<strong>$ _COOKIE<\/strong>&nbsp;.&nbsp;Yii \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 cookie \u043a\u0430\u043a \u043e\u0431\u044a\u0435\u043a\u0442 \u043a\u043b\u0430\u0441\u0441\u0430&nbsp;<strong>yii \\ web \\ Cookie<\/strong>&nbsp;.&nbsp;\u0412 \u044d\u0442\u043e\u0439 \u0433\u043b\u0430\u0432\u0435 \u043c\u044b \u043e\u043f\u0438\u0448\u0435\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0447\u0442\u0435\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u043e\u0432 cookie.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u0428\u0430\u0433 1<\/strong>&nbsp;\u2014 \u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043c\u0435\u0442\u043e\u0434&nbsp;<strong>actionReadCookies<\/strong>&nbsp;\u0432&nbsp;<strong>SiteController<\/strong>&nbsp;.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public function actionReadCookies() { \n   \/\/ get cookies from the \"request\" component \n   $cookies = Yii::$app-&gt;request-&gt;cookies; \n   \/\/ get the \"language\" cookie value \n   \/\/ if the cookie does not exist, return \"ru\" as the default value \n   $language = $cookies-&gt;getValue('language', 'ru'); \n   \/\/ an alternative way of getting the \"language\" cookie value \n   if (($cookie = $cookies-&gt;get('language')) !== null) { \n      $language = $cookie-&gt;value; \n   } \n   \/\/ you may also use $cookies like an array \n   if (isset($cookies['language'])) { \n      $language = $cookies['language']-&gt;value; \n   } \n   \/\/ check if there is a \"language\" cookie \n   if ($cookies-&gt;has('language')) echo \"Current language: $language\"; \n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u0428\u0430\u0433 2.<\/strong>\u00a0\u0427\u0442\u043e\u0431\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043e\u0442\u043f\u0440\u0430\u0432\u043a\u0443 \u0444\u0430\u0439\u043b\u043e\u0432 cookie \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438, \u0441\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043c\u0435\u0442\u043e\u0434\u00a0<strong>actionSendCookies<\/strong>\u00a0\u0432\u00a0<strong>SiteController<\/strong>\u00a0.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public function actionSendCookies() { \n   \/\/ get cookies from the \"response\" component \n   $cookies = Yii::$app-&gt;response-&gt;cookies; \n   \/\/ add a new cookie to the response to be sent \n   $cookies-&gt;add(new \\yii\\web\\Cookie([ \n      'name' =&gt; 'language', \n      'value' =&gt; 'ru-RU', \n   ])); \n   $cookies-&gt;add(new \\yii\\web\\Cookie([\n      'name' =&gt; 'username', \n      'value' =&gt; 'John', \n   ])); \n   $cookies-&gt;add(new \\yii\\web\\Cookie([ \n      'name' =&gt; 'country', \n      'value' =&gt; 'USA', \n   ])); \n} <\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u0428\u0430\u0433 3.<\/strong>&nbsp;\u0422\u0435\u043f\u0435\u0440\u044c, \u0435\u0441\u043b\u0438 \u0432\u044b \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u0442\u0435 \u043f\u043e&nbsp;<strong>\u0430\u0434\u0440\u0435\u0441\u0443 http: \/\/ localhost: 8080 \/ index.php? R = site \/ send-cookies<\/strong>&nbsp;, \u0432\u044b \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u0435, \u0447\u0442\u043e \u0444\u0430\u0439\u043b\u044b cookie \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/coderlessons.com\/wp-content\/uploads\/2019\/07\/save_cookies.png\" alt=\"\u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043f\u0435\u0447\u0435\u043d\u044c\u0435\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u0412 Yii \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 cookie \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u0430.&nbsp;\u041e\u043d \u0437\u0430\u0449\u0438\u0449\u0430\u0435\u0442 \u0444\u0430\u0439\u043b\u044b cookie \u043e\u0442 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u0430.&nbsp;\u0421\u0442\u0440\u043e\u043a\u0430 \u0445\u0435\u0448\u0430 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430 config \/ web.php \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u043a\u0430\u0436\u0434\u044b\u0439 \u0444\u0430\u0439\u043b cookie.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php \n   $params = require(__DIR__ . '\/params.php'); \n   $config = [ \n      'id' =&gt; 'basic', \n      'basePath' =&gt; dirname(__DIR__), \n      'bootstrap' =&gt; ['log'], \n      'components' =&gt; [ \n         <strong>'request' =&gt; [ \n            \/\/ !!! insert a secret key in the following (if it is empty) - this is \n            \/\/required by cookie validation \n            'cookieValidationKey' =&gt; 'ymoaYrebZHa8gURuolioHGlK8fLXCKjO', \n         ],<\/strong> \n         'cache' =&gt; [ \n            'class' =&gt; 'yii\\caching\\FileCache', \n         ], \n         'user' =&gt; [ \n            'identityClass' =&gt; 'app\\models\\User', \n            'enableAutoLogin' =&gt; true, \n         ], \n         'errorHandler' =&gt; [ \n            'errorAction' =&gt; 'site\/error', \n         ], \n         'mailer' =&gt; [ \n            'class' =&gt; 'yii\\swiftmailer\\Mailer', \n            \/\/ send all mails to a file by default. You have to set \n            \/\/ 'useFileTransport' to false and configure a transport \n            \/\/ for the mailer to send real emails. \n            'useFileTransport' =&gt; true, \n         ], \n         'log' =&gt; [ \n            'traceLevel' =&gt; YII_DEBUG ? 3 : 0, \n            'targets' =&gt; [ \n               [ \n                  'class' =&gt; 'yii\\log\\FileTarget', \n                     'levels' =&gt; ['error', 'warning'], \n                ], \n            ], \n         ], \n         'urlManager' =&gt; [ \n            \/\/'showScriptName' =&gt; false, \n            \/\/'enablePrettyUrl' =&gt; true, \n            \/\/'enableStrictParsing' =&gt; true, \n            \/\/'suffix' =&gt; '\/' \n         ], \n         'db' =&gt; require(__DIR__ . '\/db.php'), \n      ], \n      'modules' =&gt; [ \n         'hello' =&gt; [ \n            'class' =&gt; 'app\\modules\\hello\\Hello', \n         ], \n      ], \n      'params' =&gt; $params,\n   ]; \n   if (YII_ENV_DEV) { \n      \/\/ configuration adjustments for 'dev' environment \n      $config['bootstrap'][] = 'debug'; \n      $config['modules']['debug'] = [ \n         'class' =&gt; 'yii\\debug\\Module', \n      ]; \n      $config['bootstrap'][] = 'gii'; \n      $config['modules']['gii'] = [ \n         'class' =&gt; 'yii\\gii\\Module', \n      ]; \n   } \n   return $config; \n?&gt;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 cookie, \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0432 \u0434\u043b\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430&nbsp;<strong>yii \\ web \\ Request :: $ enableCookieValidation<\/strong>&nbsp;\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435&nbsp;<strong>false<\/strong>&nbsp;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0424\u0430\u0439\u043b\u044b cookie \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u0445.&nbsp;\u0412 PHP \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a \u043d\u0438\u043c \u0434\u043e\u0441\u0442\u0443\u043f \u0447\u0435\u0440\u0435\u0437 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e&nbsp;$ _COOKIE&nbsp;.&nbsp;Yii \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 cookie [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[156],"tags":[],"class_list":["post-4978","page","type-page","status-publish","hentry","category-yii"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/pages\/4978","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/comments?post=4978"}],"version-history":[{"count":1,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/pages\/4978\/revisions"}],"predecessor-version":[{"id":4979,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/pages\/4978\/revisions\/4979"}],"wp:attachment":[{"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/media?parent=4978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/categories?post=4978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bearloga.space\/uk\/wp-json\/wp\/v2\/tags?post=4978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}