Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Session problem

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

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

Page: 1
Go to End
Author Topic: 1339 Views
  • StuM
    Avatar
    Community Member
    56 Posts

    Session problem Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Session problem Link to this post

    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!

  • StuM
    Avatar
    Community Member
    56 Posts

    Re: Session problem Link to this post

    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

  • StuM
    Avatar
    Community Member
    56 Posts

    Re: Session problem Link to this post

    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

    1339 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.