GSTAR
(Omzy83)
November 25, 2012, 7:49pm
1
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
konapaz
(Konapaz)
November 25, 2012, 8:35pm
2
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
GSTAR
(Omzy83)
November 30, 2012, 10:14pm
3
I don’t understand how that would work. Anybody got any more ideas?
antoncpu
(Yakushinanton)
December 1, 2012, 6:22pm
4
$_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?
GSTAR
(Omzy83)
December 6, 2012, 9:12pm
5
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.
antoncpu
(Yakushinanton)
December 6, 2012, 9:28pm
6
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"?
GSTAR
(Omzy83)
December 6, 2012, 9:32pm
7
The supergloabal $_SESSION is undefined.
The PHPSESSID cookie only exists on ‘account’, not on root
antoncpu
(Yakushinanton)
December 6, 2012, 9:34pm
8
Seems session_start() was not called on http://www.mysite.com then.
GSTAR
(Omzy83)
December 6, 2012, 9:36pm
9
You’re right. That’s fixed the problem. Thank you very much sir!
antoncpu
(Yakushinanton)
December 6, 2012, 9:39pm
10
You’re welcome. Good luck with your project!