How to get guest user sessionid in Yii2?

10,413

Solution 1

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();

Solution 2

$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

Share:
10,413
Mohit Bhansali
Author by

Mohit Bhansali

A self confessed tech-freak, I have started my career as a feelancing consultant since my college days. It is said that when others were busy watching 'big bang theory' I was learning open source technologies. A hyperactive mind with a passion for getting acquainted with the latest technology I am the technical think-tank at Idealnodes.

Updated on July 26, 2022

Comments

  • Mohit Bhansali
    Mohit Bhansali almost 2 years

    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.