Pergunta

I'm using mysql database to store the user session data. I redirect the user to another server with the session id as a variable (GET), and then i need to retrieve the session from the DB using the ID.

code used in my remote server:

  Yii::app()->session->sessionID = $sessionID;
  Yii::app()->session->open();

the result is that the session gets overridden with no data (new session is opened). how do i retrieve the session from DB? do i need to create a model for the table or can i use YII session functionality?

thanks, Danny

Foi útil?

Solução

You don't have to create a model for session table due to CDbHttpSession relies on PDO to access database.

What is the configuration of your session component?

You specify session ID and start the session manually by calling open() method. This method sets session callback methods using PHP's session_set_save_handler function. readSession($id) is among them and according to session_set_save_handler's manual page:

This callback is called internally by PHP when the session starts or when session_start() is called.

Due to session_start creates a session or resumes the current one you have to grant that session is not started automatically before setting session's identifier. autoStart property of Yii's session instance is responsible for that and must be set to false (defaults to true).

Also an indentifier connectionID of your database connection to retrieve session data through must be specified. It's important, otherwise:

If not set, a SQLite database will be automatically created and used.

Keeping it all in mind I suppose that configuration of your session component must look like:

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

Outras dicas

php stores sessionid in cookie with this name ( PHPSESSID ) yii also store session id in browser's cookie( because it uses php default sessions). If you change PHPSESSID varaible in cookie , you will be able to change our session. Simply you must do that

setcookie('PHPSESSID',$sessionId);// where $sessionId is your session id you read from db and this session must exists.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top