- 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 - Sessions
Sessions make data accessible across various pages. A session creates a file on the server in a temporary directory where all session variables are stored. This data is available to all the pages of your web site during the visit of that particular user.
When a session starts, the following happens −
PHP creates a unique ID for that particular session.
A cookie called PHPSESSID is sent on the cpent side (to the browser).
The server creates a file in the temporary folder where all session variables are saved.
When a server wants to retrieve the value from a session variable, PHP automatically gets the unique session ID from the PHPSESSID cookie. Then, it looks in its temporary directory for the needed file.
To start a session, you should call the session_start() function. All session variables are stored in the $_SESSION global variable. You can also use the isset() function to check whether the session variable is set −
<?php session_start(); if( isset( $_SESSION[ number ] ) ) { $_SESSION[ number ] += 1; }else { $_SESSION[ number ] = 1; } $msg = "This page was visited ". $_SESSION[ number ]; $msg .= "in this session."; echo $msg; ?>
To destroy a session, you should call the session_destroy() function. To destroy a single session variable, call the unset() function −
<?php unset($_SESSION[ number ]); session_destroy(); ?>
Using Sessions in Yii
Sessions allow data to be persisted across user requests. In PHP, you may access them through the $_SESSION variable. In Yii, you can get access to sessions via the session apppcation component.
Step 1 − Add the actionOpenAndCloseSession method to the SiteController.
pubpc function actionOpenAndCloseSession() { $session = Yii::$app->session; // open a session $session->open(); // check if a session is already opened if ($session->isActive) echo "session is active"; // close a session $session->close(); // destroys all data registered to a session $session->destroy(); }
In the above code, we get the session apppcation component, open a session, check whether it is active, close the session, and finally destroy it.
Step 2 − Type http://localhost:8080/index.php?r=site/open-and-close-session in the address bar of the web browser, you will see the following.
To access session variables, you may use set() and get() methods.
Step 3 − Add an actionAccessSession method to the SiteController.
pubpc function actionAccessSession() { $session = Yii::$app->session; // set a session variable $session->set( language , ru-RU ); // get a session variable $language = $session->get( language ); var_dump($language); // remove a session variable $session->remove( language ); // check if a session variable exists if (!$session->has( language )) echo "language is not set"; $session[ captcha ] = [ value => aSBS23 , pfetime => 7200, ]; var_dump($session[ captcha ]); }
Step 4 − Go to http://localhost:8080/index.php?r=site/access-session, you will see the following.
Advertisements