Home>
<?php
//Setting method
$cookie=new cookie ([
  "name" =>"cookie_monster",  "value" =>"me want cookie!",  "expire" =>time () + 86400 * 365,]);
\ yii ::$app->getresponse ()->getcookies ()->add ($cookie);
//Read method
$value=\ yii ::$app->getrequest ()->getcookies ()->getvalue ("my_cookie");
//Add a domain name to the cookie
$cookie=new cookie ([
  "name" =>"cookie_monster",  "value" =>"me want cookie everywhere!",  "expire" =>time () + 86400 * 365,  "domain" =>".example.com" //<<<=== here
]);
\ yii ::$app->getresponse ()->getcookies ()->add ($cookie);
//Set login cookie
$config=[
  //...
  "components" =>[
    //...
    "user" =>[
      "class" =>"yii \ web \ user",      "identityclass" =>"app \ models \ user",      "enableautologin" =>true,      "loginurl" =>"/user/login",      "identitycookie" =>[//<---- here!
        "name" =>"_identity",        "httponly" =>true,        "domain" =>".example.com",      ],    ],    "request" =>[
      "cookievalidationkey" =>"your_validation_key"
    ],    "session" =>[
      "cookieparams" =>[
        "domain" =>".example.com",        "httponly" =>true,      ],    ],  ],];
//Configure cookies only for the approved directory
$config=[
  //...
  "components" =>[
    //...
    "session" =>[
      "name" =>"admin_session",      "cookieparams" =>[
        "httponly" =>true,        "path" =>"/admin",      ],    ],  ],];
?>
php
  • Previous Yii 20 implementation to create simple widgets example
  • Next PHP implementation of 301 redirect jump example code