YII Sessions

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Sessions

Sessions allow us to get access to data across various pages for each user request. A file is created in a temporary directory on the server by session to store all session variables. This stored data is available to all the pages of a web site during the visit of a particular user.

In PHP, session is accessed through the $_SESSION global variable.

When a session starts,

  • A unique ID is generated for that particular session.

  • A cookie is sent to the client side.

  • All these session variables are saved in the temporary folder on the server.

  • When a value is retrived from the session variable, it automatically gets unique session ID from the cookie. Then it looks into its temporary folder for the particular file.


Sessions Opening and Closing

Let's see an example of opening and closing a session.

Step 1 Go to the SiteController.php file. Add the action actionCheckStatus.

  1. public function actionCheckStatus()   
  2.    {   
  3.   $session = Yii::$app->session;   
  4.   // open a session   
  5.   $session->open();   
  6.   // check if a session is already active   
  7.   if ($session->isActive) echo "Session is Active";   
  8.   // close a session   
  9.   $session->close();   
  10.   // destroys all data registered to a session   
  11.   $session->destroy();   
  12.    }  

Look at the above code, it shows session opening, session closing, checks whether session is active or not and destroys the session.

Step 2 Run it on the browser with the URL,

http://localhost/sess/frontend/web/index.php?r=site/check-status

YII Session 1

Accessing Session Data

During accessing of the data, if no session is running than session will automatically start it.

To access the data stored in the session, run the following code.

  1. $session = Yii::$app->session;  

  2.   

  3. // get a session variable. The following usages are equivalent:  

  4. $language = $session->get('language');  

  5. $language = $session['language'];  

  6. $language = isset($_SESSION['language']) ? $_SESSION['language'] : null;  

  7.   

  8. // set a session variable. The following usages are equivalent:  

  9. $session->set('language', 'en-US');  

  10. $session['language'] = 'en-US';  

  11. $_SESSION['language'] = 'en-US';  

  12.   

  13. // remove a session variable. The following usages are equivalent:  

  14. $session->remove('language');  

  15. unset($session['language']);  

  16. unset($_SESSION['language']);  

  17.   

  18. // check if a session variable exists. The following usages are equivalent:  

  19. if ($session->has('language')) ...  

  20. if (isset($session['language'])) ...  

  21. if (isset($_SESSION['language'])) ...  

  22.   

  23. // traverse all session variables. The following usages are equivalent:  

  24. foreach ($session as $name => $value) ...  

  25. foreach ($_SESSION as $name => $value) ...