Yii2 Session is already started

Hi,

I’m getting a lot of warning in the logs like :

[-][-][warning][yii\web\Session::init] Session is already started

This happens for every $session = Yii::$app->session; at least that is the line number reported in the warning

I was trynig to detect active session libke this:

$session = Yii::$app->session;

if (!$session->isActive)

$session->open();

$session_id=$session->getId();

but I’m still getting those warning, as those warning have quite a bit of troubleshooting information this logs grow quite big on a busy side.

What is the proper way of testing if session is already started?

$session->isActive is just wrapper around session_status(), could be a something small are you trying to start a session yourself else where in your application, may be check for possible session_start() function calls.

Also you don’t to need to start a session manually yii will start a session the moment the session component is initialized.

Are you saying I don’t need to run $session->open() at any point?

yes exactly you don’t need to open the session and fiddle with lower level api unless really necessary, just use the higher level api’s provided. here is an example


// to set a value

Yii::$app->session->set('foo', 'bar');

// or get a value

Yii::$app->session->get('foo');

both set, get call open internally so you use that.