Mohit Bhansali
Mohit Bhansali

Reputation: 1785

How to get guest user sessionid in Yii2?

I have used YII's session_id's to track user activity across a website with the following code snippet:

Yii::app()->getSession()->getSessionId()

How do I get same sessionid in Yii2? I tried quite possible way, but its all vain.

Please share the exact code snippet for Yii2.

Upvotes: 2

Views: 12226

Answers (3)

mahfuz
mahfuz

Reputation: 3223

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

// if session is not open, open session
if ( !$session->isActive) { $session->open(); }

// get session id
Yii::$app->session->getId();

I got 26 character string in session id. "itddoa1lr247phpds34aemr0v0"

This link can be helpful: http://www.yiiframework.com/doc-2.0/guide-runtime-sessions-cookies.html#sessions

Upvotes: 1

Lapenkov Vladimir
Lapenkov Vladimir

Reputation: 3218

Yii::$app->session->Id

And no need to explicitly open session. When you access session data through the session component, a session will be automatically opened if it has not been done so before. http://www.yiiframework.com/doc-2.0/guide-runtime-sessions-cookies.html#sessions

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133360

You can try with

Yii::$app->session->getId();

this guide could be useful to you http://www.yiiframework.com/doc-2.0/guide-runtime-sessions-cookies.html

try checking if a session is already active before and ifnot use session open() .

session = Yii::$app->session;

// check if a session is already open
if ($session->isActive) ...

// open a session
$session->open();

// close a session
$session->close();

// destroys all data registered to a session.
$session->destroy();

Upvotes: 5

Related Questions