2013-12-23 186 views
2

我有yii項目,Chrome會話丟失。 實施例: 配置在main.phpYii在重定向後丟失會話

'session' => array(
      'class' => 'CDbHttpSession', 
      'autoStart' => false, 
      'connectionID' => 'db', 
      'sessionTableName' => 'ph_YiiSession', 
      'autoCreateSessionTable' => false // for performance reasons 
     ), 

在啓動控制器登錄後我寫在會話

Yii::app()->user->id = 100 

ID用戶i之後重定向用戶

$this->redirect(array('student/index'), true); 

但在索引動作我無法從會話中獲取數據

echo Yii::app()->user->id; 

什麼也不給。請幫幫忙,這個問題墜毀我的大腦已經

+0

你想做什麼? Yii :: app() - > user-> id是如何與會話相關的? –

+0

請張貼您的身份驗證碼... –

+0

http://pastebin.com/bBV1J4XN – 6yt9Bka

回答

0

你應該嘗試

'autoStart' => true, 
+0

我試過了,沒有幫助 – 6yt9Bka

+0

可能這會幫助你。 http://php.refulz.com/configuring-session-in-yii-framework-cdbhttpsession/ –

+0

沒有幫助,我看錶ph_YiiSession並有1行,但在'數據'列沒有。 – 6yt9Bka

0
Yii::app()->user->id = 100 

首先

您不能設置標識這樣永久,可以在不同的工作頁面。即使您將頁面設置爲ID,只要移至下一頁,其數據就會丟失,並且會顯示其默認值。所以如果你想改變Yii::app()->user->id包含的值,那麼你將不得不重寫getId()方法。

第二件事

如果你要保存的Id的會話,那麼你應該使用Yii::app()->session['_myId']=Yii::app()->user->id; 然後你就可以得到它像

echo Yii::app()->session['_myId']; 

,並記住'autoStart' => TRUE,

+0

1.我以它爲例來展示。 2.我已經做了(使用會話['_ myId']),不工作,在數據庫中 - 什麼都沒有。 – 6yt9Bka

+0

你能告訴我如何在會話中保存數據 –

+0

例如:在站點控制器中我寫了Yii :: app() - > session ['value'] ='12345';重定向到其他控制器後:echo Yii :: app() - > session ['value']; - 沒什麼頁面,沒有在分貝 – 6yt9Bka

0

什麼你做錯了,肯定是在UserIdentity類。從Yii::app()->user->id設置和檢索會話數據的最佳方法是覆蓋UserIdentity類中的getId()方法。

例如,假設您有一個名爲'User'的表,它包含:id,username,password。

因此,使UserIdentity類是這樣的:

<?php 

/** 
* UserIdentity represents the data needed to identity a user. 
* It contains the authentication method that checks if the provided 
* data can identity the user. 
*/ 
class UserIdentity extends CUserIdentity 
{ 

    private $_id; 

    public function authenticate() 
    { 
     $user = User::model()->find('LOWER(username)=?',array(strtolower($this->username))); 
     if($user===null){ 
      $this->errorCode=self::ERROR_USERNAME_INVALID; 

     }else if($user->password !== crypt($this->password,$user->password)){ 
      $this->errorCode = self::ERROR_PASSWORD_INVALID; 
     } 
     else{ 
      $this->_id = $user->id; 
      $this->username = $user->username; 
      $this->errorCode = self::ERROR_NONE; 
     } 
     return $this->errorCode === self::ERROR_NONE; 
    } 

    public function getId() 
    { 
     return $this->_id; 
    } 
} 

一旦你這樣做,你應該能夠使用的Yii ::應用程序() - >用戶> ID和獲取會話ID代碼中的任何地方。

希望這有助於。

P.S>我也做了一個基地的應用程序,所有這些都已完成。您可以查看:https://github.com/sankalpsingha/yii-base-app它可能會幫助你。

+0

我的UserIdentity:http://pastebin.com/vcELsG2k,THX但沒有幫助。 – 6yt9Bka

+0

在過程中我發現文件會話存儲(我從CDbHttpSession更改爲CHttpSession),但其中的數據不是。在這個環境中存在使用會話的其他應用程序並且沒問題。這對我來說很神奇。 – 6yt9Bka