Introduction to the session instance of Yii operation _php Instance

Source: Internet
Author: User

This article briefly describes the method of using session in the YII framework, as follows:

One, and the standard PHP code difference:

In the YII framework, you don't need to use session_start () like standard PHP code.
In the YII framework, the AutoStart property is set to True by default, so
Although Session_Start () is not used, you can still use $_session global variables, but it is best to use the yii::app->session of the YII framework package

Second, the 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 when using a more complex point
The configuration items can be located in the protected/config/main.php components :

' Session ' =>array (
  ' AutoStart ' =>false (/true),
  ' sessionname ' => ' Site Access ',
  ' cookiemode ' = > ' only ',
  ' savepath ' = '/path/to/new/directory ',
),

To keep the session in the database settings:

' Session ' => Array (
  ' class ' => ' system.web.CDbHttpSession ', '
  connectionid ' => ' db ',
  ' Sessiontablename ' => ' actual_table_name ',
),

In addition, for debugging purposes, it is sometimes necessary to know the session ID of the current user,
This value is in:

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

Finally, when the user exits login (logout), you need to remove traces that can be used:

 
 

Remove all session variables, and then call the

 
 

Remove the session data stored on the server side.

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.