Yii2 open session
Use yii \ web \ Session;
The code is as follows: |
Copy code |
$ Session = Yii: $ app-> session; // Check if a session is already open If ($ session-> isActive )... // Open a session $ Session-> open (); // Close a session $ Session-> close (); // Destroys all data registered to a session. $ Session-> destroy (); |
Set session
The code is as follows: |
Copy code |
$ Session = Yii: $ app-> session; $ Session-> set ('User _ id', '123 '); // OR $ Session ['User _ id'] = '000000 '; // OR $ _ SESSION ['User _ id'] = '000000 '; |
Read session
The code is as follows: |
Copy code |
$ Session = Yii: $ app-> session; $ User_id = $ session-> get ('User _ id '); // OR $ User_id = $ session ['User _ id']; // OR $ User_id = isset ($ _ SESSION ['User _ id'])? $ _ SESSION ['User _ id']: null;
|
Destroy session
The code is as follows: |
Copy code |
$ Session = Yii: $ app-> session; $ Session-> remove ('User _ id '); // OR Unset ($ session ['User _ id']); // OR Unset ($ _ SESSION ['User _ id']); |
Read and set an array session
The code is as follows: |
Copy code |
$ Session = Yii: $ app-> session; $ Session ['user'] = [ 'Id' => 1, 'Username' => 'iiuser ', ]; Echo $ session ['user'] ['id']; Echo $ session ['user'] ['username']; $ Session ['user. Id'] = 1; $ Session ['user. Username'] = 'iiuser '; |