Yii session instance introduction, yiisession instance _ PHP Tutorial

Source: Internet
Author: User
Yii operation session instance introduction, yiisession instance. Yii operation session instance introduction, yiisession instance this article describes how to use session in the Yii Framework, the specific steps are as follows: 1. differences with standard php code: In the Yii Framework, introduction to session instances without yii operations, yiisession instances

This article briefly describes how to use session in the Yii Framework. the specific steps are as follows:

I. differences from standard php code:

In the Yii Framework, you do not need to use session_start () like the standard PHP code (),
In the Yii Framework, the autoStart attribute is set to true by default. therefore,
Although session_start () is not used, you can still use the $ _ SESSION global variable, but it is best to use the Yii: app-> session encapsulated by the Yii Framework.

II. 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 in a more complex way
Configuration items can be located inComponents of protected/config/main. phpMedium:

'session'=>array(  'autoStart'=>false(/true),  'sessionName'=>'Site Access',  'cookieMode'=>'only',  'savePath'='/path/to/new/directory',),

Keep the session settings in the database:

'session' => array (  'class' => 'system.web.CDbHttpSession',  'connectionID' => 'db',  'sessionTableName' => 'actual_table_name',),

In addition, for debugging, you sometimes need to know the session ID of the current user,
The value is in:

Yii::app()->session->sessionID

Finally, when the user logs out, you need to eliminate the trace, you can use:

Yii::app()->session->clear() 

Remove all session variables, and then call

Yii::app()->session->destroy() 

The session data stored on the server is removed.


How does yii set the session time? You can set help3ks in mainphp or on the page.

You must first. set session timeout in php, 'sessiontimeoutseconds '=> 300, and then in yii: app ()-> user-> setState ('usersessiontimeout', time () + Yii :: app ()-> params ['sessiontimeoutseconds ']); call the page.
Hope to help you
 

How to configure SESSION in yii

Is it okay to use the session directly?
 

This article describes how to use session in the Yii Framework. the specific steps are as follows: 1. differences from standard php code: In the Yii Framework, you do not...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.