yii2開啟session
useyii\web\Session; $session = Yii::$app->session;// check if a session is already openif($session->isActive) ...// open a session$session->open();// close a session$session->close();// destroys all data registered to a session.$session->destroy();設定session$session = Yii::$app->session;$session->set('user_id', '1234');//OR$session['user_id'] = '1234';//OR$_SESSION['user_id'] = '1234';讀取session$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;銷毀session$session = Yii::$app->session;$session->remove('user_id');//ORunset($session['user_id']);//ORunset($_SESSION['user_id']);數組session的讀取與設定$session = Yii::$app->session;$session['user'] = ['id' => 1, 'username' => 'yiiuser',];echo$session['user']['id'];echo$session['user']['username']; $session['user.id'] = 1;$session['user.username'] = 'yiiuser';
轉自:http://www.kuitao8.com/20141213/3351.shtml
以上就介紹了yii2 session的使用方法,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。