Yii Tutorial on 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 client 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 application component.

step 1 − add the actionopenandclosesession method to the sitecontroller.

public 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 application 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.

session active

to access session variables, you may use set() and get() methods.

step 3 − add an actionaccesssession method to the sitecontroller.

public 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',
      'lifetime' => 7200,
   ];
   var_dump($session['captcha']);
}

step 4 − go to http://localhost:8080/index.php?r=site/access-session, you will see the following.

action session