Home  >  Article  >  Backend Development  >  Introduction to yii operation session examples, yiisession examples_PHP tutorial

Introduction to yii operation session examples, yiisession examples_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:21:45770browse

Introduction to yii operation session examples, yiisession examples

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

encapsulated by the Yii framework

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.

How to set the session time in yii? Set it in mainphp, or you can also set help3ks

within the page

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

How to configure SESSION in yii

Can’t session be used directly?

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/854352.htmlTechArticleIntroduction to yii operation session examples, yiisession examples. This article briefly describes the method of using sessions in the Yii framework. The specific steps are as follows: 1. Differences from standard PHP code: In the Yii framework, you don’t...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn