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.

General Questions /

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

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

creating a logout link


Go to End


10 Posts   14382 Views

Avatar
anujkryadav

Community Member, 30 Posts

19 December 2009 at 2:06am

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

Avatar
anujkryadav

Community Member, 30 Posts

19 December 2009 at 2:17am

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

Avatar
yurigoul

Community Member, 203 Posts

19 December 2009 at 2:49am

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...

Avatar
anujkryadav

Community Member, 30 Posts

19 December 2009 at 6:35am

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

Avatar
yurigoul

Community Member, 203 Posts

19 December 2009 at 12:06pm

Edited: 19/12/2009 1:15pm

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.

Avatar
Willr

Forum Moderator, 5523 Posts

19 December 2009 at 2:01pm

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.

Avatar
anujkryadav

Community Member, 30 Posts

21 December 2009 at 6:05pm

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

Avatar
Anatol

126 Posts

19 August 2010 at 5:11pm

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

Go to Top