Jump to:

23007 Posts in 11866 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » creating a logout link

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1 2
Go to End
Author Topic: 6277 Views
  • anujkryadav
    Avatar
    Community Member
    30 Posts

    creating a logout link Link to this post

    I have created a form for registration which is working fine,
    I am using default login form to make user login to access a particular page,
    When the user enter username and password he is directed to a particular page(Download page).
    All this are working fine.
    Now I want to have a LOGOUT link in my page(download page),When a user click on logout he should be directed to home page.
    Please help me creating a logout link which redirect to home page.
    Thank you in advance

  • anujkryadav
    Avatar
    Community Member
    30 Posts

    Re: creating a logout link Link to this post

    I have tried using login widgets but when I click on logout there its redirect to the same page adn also have tried with /secure/logout. that also does not redirect to homepage

  • yurigoul
    Avatar
    Community Member
    202 Posts

    Re: creating a logout link Link to this post

    With Director::redirect() you can redirect someone to another page. I would then test if someone is logged in and if not use a redirect. That is where I would start...

  • anujkryadav
    Avatar
    Community Member
    30 Posts

    Re: creating a logout link Link to this post

    Can you please explain me in detail how to use it,I simple need to open homepage when my user click on logout

  • yurigoul
    Avatar
    Community Member
    202 Posts

    Re: creating a logout link Link to this post

    I am just guessing.

    Here is more info on redirect: http://doc.silverstripe.org/doku.php?id=director

    Here for member: http://doc.silverstripe.org/doku.php?id=member

    My guess is you shout be able to do something with this info...

    if( $member = Member::currentUser() ) {
       // Work with $member
    } else {
       // Do non-member stuff
    }

    Or you create your own logout function, but I have at this moment no idea where to start.

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: creating a logout link Link to this post

    A simple Logout method

    // mysite/code/Page.php -> Page_Controller

    function logout() {
       Security::logout(false);
       Director::redirect("home/");
    }

    Then you can have a link like <a href="home/logout">Logout</a> in your template. That function logs the member out then redirects them to the 'home' url.

  • anujkryadav
    Avatar
    Community Member
    30 Posts

    Re: creating a logout link Link to this post

    thank you Willr the function is working and is redirecting user to the home page when someone click on logout but the session is not logged out,the session is still logged in in other page as i have used $LoginForm in my homepage ,so when a user click on logout the form should be visible in homepage but that is not happening..instead of form it is showing as user logged in

  • Anatol
    Avatar
    126 Posts

    Re: creating a logout link Link to this post

    Hi,

    I tried this in Silverstripe 2.4 - you need to add to Page_Controller

    public static $allowed_actions = array (
       'logout'
    );

    public function logout() {
       Director::redirect("home/");
       Security::logout(true);
    }

    I also changed the order: Director::redirect first, then the logout. This seems to work better. My main problem was that when a logged in user was on a special page like the own user profile of the forum and the user would log out from that page this would give a fatal error. Redirect to home and logout then is safer.

    Cheers!
    Anatol

    6277 Views
Page: 1 2
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.