- Gii – Generating Module
- Gii – Generating Controller
- Gii – Creating a Model
- Yii - Gii
- Yii - Localization
- Yii - Authorization
- Yii - Authentication
- Yii - Error Handling
- Yii - Logging
- Yii - Aliases
- Yii - Fragment Caching
- Yii - Caching
- Yii - Testing
- Yii - Fields
- Yii - RESTful APIs in Action
- Yii - RESTful APIs
- Yii - Theming
- Yii - Database Migration
- Yii - Active Record
- Yii - Query Builder
- Yii - Data Access Objects
- Yii - Database Access
- Yii - Dependency Injection
- Yii - Configurations
- Yii - Creating a Behavior
- Yii - Behaviors
- Yii - Creating Event
- Yii - Events
- Yii - GridView Widget
- Yii - ListView Widget
- Yii - Data Widgets
- Yii - Data Providers
- Yii - Properties
- Yii - Sorting
- Yii - Pagination
- Yii - Formatting
- Yii - Files Upload
- Yii - Using Cookies
- Yii - Cookies
- Yii - Using Flash Data
- Yii - Sessions
- Yii - AJAX Validation
- Yii - Ad Hoc Validation
- Yii - Validation
- Yii - HTML Forms
- Yii - Rules of URL
- Yii - URL Routing
- Yii - URL Formats
- Yii - Responses
- Yii - HTTP Requests
- Yii - Creating Extensions
- Yii - Extensions
- Yii - Asset Conversion
- Yii - Assets
- Yii - Layouts
- Yii - Views
- Yii - Modules
- Yii - Widgets
- Yii - Models
- Yii - Using Actions
- Yii - Using Controllers
- Yii - Controllers
- Yii - Entry Scripts
- Yii - Application Structure
- Yii - Create Page
- Yii - Installation
- Yii - Overview
- Yii - Home
Yii Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Yii - Using Cookies
Cookies allow data to be persisted across requests. In PHP, you may access them through the $_COOKIE variable. Yii represents cookie as an object of the yiiwebCookie class. In this chapter, we describe several methods for reading cookies.
Step 1 − Create an actionReadCookies method in the SiteController.
pubpc function actionReadCookies() { // get cookies from the "request" component $cookies = Yii::$app->request->cookies; // get the "language" cookie value // if the cookie does not exist, return "ru" as the default value $language = $cookies->getValue( language , ru ); // an alternative way of getting the "language" cookie value if (($cookie = $cookies->get( language )) !== null) { $language = $cookie->value; } // you may also use $cookies pke an array if (isset($cookies[ language ])) { $language = $cookies[ language ]->value; } // check if there is a "language" cookie if ($cookies->has( language )) echo "Current language: $language"; }
Step 2 − To see sending cookies in action, create a method called actionSendCookies in the SiteController.
pubpc function actionSendCookies() { // get cookies from the "response" component $cookies = Yii::$app->response->cookies; // add a new cookie to the response to be sent $cookies->add(new yiiwebCookie([ name => language , value => ru-RU , ])); $cookies->add(new yiiwebCookie([ name => username , value => John , ])); $cookies->add(new yiiwebCookie([ name => country , value => USA , ])); }
Step 3 − Now, if you go to http://localhost:8080/index.php?r=site/send-cookies, you will notice that cookies are saved inside the browser.
In Yii, by default, cookie vapdation is enabled. It protects the cookies from being modified on the cpent side. The hash string from the config/web.php file signs each cookie.
<?php $params = require(__DIR__ . /params.php ); $config = [ id => basic , basePath => dirname(__DIR__), bootstrap => [ log ], components => [ request => [ // !!! insert a secret key in the following (if it is empty) - this is //required by cookie vapdation cookieVapdationKey => ymoaYrebZHa8gURuopoHGlK8fLXCKjO , ], cache => [ class => yiicachingFileCache , ], user => [ identityClass => appmodelsUser , enableAutoLogin => true, ], errorHandler => [ errorAction => site/error , ], mailer => [ class => yiiswiftmailerMailer , // send all mails to a file by default. You have to set // useFileTransport to false and configure a transport // for the mailer to send real emails. useFileTransport => true, ], log => [ traceLevel => YII_DEBUG ? 3 : 0, targets => [ [ class => yiilogFileTarget , levels => [ error , warning ], ], ], ], urlManager => [ // showScriptName => false, // enablePrettyUrl => true, // enableStrictParsing => true, // suffix => / ], db => require(__DIR__ . /db.php ), ], modules => [ hello => [ class => appmoduleshelloHello , ], ], params => $params, ]; if (YII_ENV_DEV) { // configuration adjustments for dev environment $config[ bootstrap ][] = debug ; $config[ modules ][ debug ] = [ class => yiidebugModule , ]; $config[ bootstrap ][] = gii ; $config[ modules ][ gii ] = [ class => yiigiiModule , ]; } return $config; ?>
You can disable cookie vapdation by setting the yiiwebRequest::$enableCookieVapdation property to false.
Advertisements