This article briefly describes the method of using session in Yii framework. The specific steps are as follows:
1. Differences from standard php code:
In Yii framework, you don’t need to use session_start() like standard PHP code,
In the Yii framework, the autoStart attribute is set to true by default, so,
Although session_start() is not used, you can still use the $_SESSION global variable, but it is best to use Yii::app->session
2. Use of session variables:
Set session:
Yii::app()->session['var']='value';
Use session:
echo Yii::app()->session['var'];
Remove session:
unset(Yii::app()->session['var']);
How to configure your session for more complicated use
Configuration items can be set in components of protected/config/main.php:
'session'=>array( 'autoStart'=>false(/true), 'sessionName'=>'Site Access', 'cookieMode'=>'only', 'savePath'='/path/to/new/directory', ),
Keep session in database settings:
'session' => array ( 'class' => 'system.web.CDbHttpSession', 'connectionID' => 'db', 'sessionTableName' => 'actual_table_name', ),
In addition, for debugging, sometimes you need to know the session ID of the current user,
The value is at:
Yii::app()->session->sessionID
Finally, when the user logs out (logout), you need to eliminate traces, you can use:
Yii::app()->session->clear()
Remove all session variables, then call
Yii::app()->session->destroy()
Remove session data stored on the server side.
You first need to set the session timeout in main.php, 'sessionTimeoutSeconds'=>300, and then in yii::app()->user->setState('userSessionTimeout',time()+Yii: :app()->params['sessionTimeoutSeconds']); page to call.
Hope this helps you
Can’t session be used directly?