Access Session Variable Outside Of Yii

Is it possible I can set a session variable in Yii and access that variable outside the Yii application?

Basically what I have is a Yii app in a subdirectory of my website (http://www.mysite.com/account)

On my site header I have login / logout / my account / register links - I need to show / hide these links based on whether a user is logged in to the Yii app or not.

The issue is that since the Yii app doesn’t get loaded outside the ‘account’ subdirectory, the site cannot access Yii properties, such as Yii::app()->user->isGuest

How can I set a site-wide session variable that does not interfere with Yii’s own session handling?

1 Like

Hi GSTAR

If I understood well, You want to get the state of session of user Yii, but outside of Yii site, right?

What I think

Make a special controller/action which is accessed only from http://www.mysite.com

for example checking the $_SERVER[‘HTTP_REFERER’] and return by json or by serialize the Yii session state (may encrypted)

The request from http://www.mysite.com to http://www.mysite.com/account may have pid session (or cookies) to represents a directed request of client (User)

Having the state of session you can do what you want

I don’t understand how that would work. Anybody got any more ideas?

$_SESSION variable should already be site-wide. Why just not to set what you need using Yii::app()->session->add() and retrieve this information from $_SESSION?

Or do you have some specific server settings?

That doesn’t seem to work for me.

In my login script, I put in the following code:


Yii::app()->session->add('loggedIn', true);

In my site header file, I have done a print_r() of $_SESSION.

When I am on http://www.mysite.com/account I can see the contents of $_SESSION, including my new ‘loggedIn’ variable.

When I go to http://www.mysite.com it says $_SESSION is undefined.

I am testing this on a localhost apache server.

Do you mean $_SESSION[‘loggedIn’] undefined or superglobal $_SESSION?

Also could you check your browser PHPSESSID cookie parameter? Does it change when you move from root to "account"?

The supergloabal $_SESSION is undefined.

The PHPSESSID cookie only exists on ‘account’, not on root

Seems session_start() was not called on http://www.mysite.com then.

You’re right. That’s fixed the problem. Thank you very much sir!

You’re welcome. Good luck with your project!