Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo

Session problem


Go to End


4 Posts   1871 Views

Avatar
StuM

Community Member, 59 Posts

12 September 2008 at 4:34pm

Edited: 12/09/2008 4:40pm

Hi All,

I have a Page_Controller class that extends ContentController. I tried to get the current user id by calling Member::currentUserId(), but it always returns 0. I looked at the code and saw that it called Session::get('loggedInAs'), so I tried calling that from my controller, still empty. If I print_r($_SESSION) it's there. Have I missed some trick or something that disallows sessions from ContentControllers?

My code is:

        public function LoginLink()
	{
		$id = Member::currentUserID();
		if ($id)
		{
			if (Member::isInGroup(1))
			{
				$link = 'admin';
			}
			else if (Member::isInGroup(2))
			{
				$link = 'partner-login';
			}
			else
			{
				$link = 'home';
			}
			return sprintf('<a href="%s%s">My Account</a>', Director::baseURL(), $link);
		}
		return '';
	}

and it's called from inside the template

Avatar
Willr

Forum Moderator, 5523 Posts

12 September 2008 at 9:13pm

If you do a Debug::show(Member::currentMember()); you don't get anything at all? and your logged in?. Heh Ive done that trick before work out why I wasnt 'logged in' when I actually wasnt!

Avatar
StuM

Community Member, 59 Posts

13 September 2008 at 10:22am

Tried the debug and nothing at all, just an empty white stripe at the top of the page. And yes, logged in, this is the results of print_r($_SESSION) from the same function:

Array
(
    [FormInfo] => Array
        (
            [GFLoginForm_LoginForm] => 
            [OpenIDLoginForm_LoginForm] => 
            [Form_EditorToolbarImageForm] => 
            [Form_EditorToolbarLinkForm] => 
            [Form_EditorToolbarFlashForm] => 
        )

    [MemberLoginForm] => Array
        (
            [force_message] => 
        )

    [OpenIDLoginForm] => Array
        (
            [force_message] => 
        )

    [lang] => Array
        (
            [site] => en
            [cms] => en
        )

    [BackURL] => 
    [Security] => Array
        (
            [Message] => Array
                (
                    [message] => Welcome Back, Stu
                    [type] => good
                )

        )

    [loggedInAs] => 1
    [SecurityID] => 652091318
)

I don't really wanna access $_SESSION directly for this, but looks like I'll have to

Avatar
StuM

Community Member, 59 Posts

15 September 2008 at 9:22am

OK, done more experimenting, I can make it work with:

$m = Member::currentUser();
$id = $m->getField('ID');
if ($id)
{ ... }

but, it works from all pages but the home page, so I had to change it to:

$m = Member::currentUser();
if ($m && $id = $m->getField('ID'))
{ ... }

this seems pretty crazy, why aren't session variables available via Session::get() from the homepage only.

The home page does have it's own controller, that extends the page controller, I've created a test page that uses the homepage controller, and it works, only difference in the pages are that there is a page segment for one of them