1. Check whether the session is enabled

In the YII framework, \ yII ::The session class, we can use it directly, but we need to judge it before we use itThe session->open() method uses the session. The following code

public function actionSession() {$session = \Yii::$app->session;
    if($session->isActive){
        echo 'Session is open';
    }else{
        $session->open();
        echo 'Session is not open, code is manually open'; }}Copy the code

Session assigns the set method

$session->set('user'.'xiaoming'); or$session['user'] = 'Joe';
Copy the code

Session Value get method

echo $session->get('user'); orecho $session['user']
Copy the code

Delete the session

$session->remove('user'); orunset($session['user']);
Copy the code

supplement

The YII framework stores sessions by browser. Each browser has a separate session file, so if you store a session value in Chrome, you won’t get it in IE.