CookiesA cookie is a small file that server embeds in the user's system which is used to identify a user. In Yii, each cookie is an object of yii\web\Cookie. The yii\web\Request (Collection of submitted cookies in a request) and yii\web\Response (Collection of cookies that need to be sent to a user) maintains the collection of cookies via the property named cookies. Controller deals with the cookies request and response in an application. Hence, cookies should be read and sent in controller. Setting CookiesSend cookies to the end users using the following codes. Getting CookiesTo get a cookie use the following code. Removing CookiesTo remove a cookie, use remove() function of Yii. Example: Let's see an example to set and show cookie's value. Step 1 Add the two actions actionSetCookie and actionShowCookie in the SiteController.php file. Step 2 Run it on the browser to set the cookie first with following URL, http://localhost/cook/frontend/web/index.php?r=site/set-cookie Step 3 Run it on the browser to show the cookie with following URL, http://localhost/cook/frontend/web/index.php?r=site/show-cookie Next Topic# |