tuxy Posted March 6, 2015 Share Posted March 6, 2015 Hello, I added this code: <?php if($user->isLoggedin()) { // if user logged in, show Logout-link! echo "<a href='login/login/logout'>Logout</a>"; }; If the user logout, then redirect to the same page. How can I do that? Grtz, Christophe Link to comment Share on other sites More sharing options...
mr-fan Posted March 6, 2015 Share Posted March 6, 2015 here we go: https://processwire.com/talk/topic/107-custom-login/#entry735 <?php //color your code! if($user->isLoggedin()) { // user is already logged in, so they don't need to be here $session->redirect("/somewhere/"); } // check for login before outputting markup if($input->post->user && $input->post->pass) { $user = $sanitizer->username($input->post->user); $pass = $input->post->pass; if($session->login($user, $pass)) { // login successful $session->redirect("/somewhere/"); } } Link to comment Share on other sites More sharing options...
Alexander Posted March 6, 2015 Share Posted March 6, 2015 It's easy Add to any page if ($user->isLoggedin()){ //get link with current page id echo "<a href='/logout" . "?redirect=$page->id" . "'>Logout</a>"; } To logout.php <?php //get page id from input $redirect = $sanitizer->name(wire('input')->get->redirect); //get url from page id $url = $pages->get($redirect)->url; if($user->isLoggedin()) $session->logout(); $session->redirect($url); ?> 2 Link to comment Share on other sites More sharing options...
Alexander Posted March 6, 2015 Share Posted March 6, 2015 @mr-fan Fastest gun in Bavaria. You post login, not logout code and "/somewhere/" never redirect you to the same page. 1 Link to comment Share on other sites More sharing options...
tuxy Posted March 6, 2015 Author Share Posted March 6, 2015 Hi guys, Thanks for the fast answers. I like it Alexander, I added your code and added a logout.php, but after redirect, it shows a 404 page. Thats the link after click on logout: http://mysite.com/logout?redirect=1 I'm a newbie and not so easy for me this code. I've got the basics, but thats it But I learned Christophe Link to comment Share on other sites More sharing options...
Alexander Posted March 6, 2015 Share Posted March 6, 2015 @tuxy Make sure that you put right URL to your logout page in "<a href='/logout'>". I recommend you to use ProcessWire API to get right URL. <?php echo "<a href='" . $pages->get("id=33977")->url . "?redirect=$page->id". "'>Logout</a>"; ?> 33977 change to your logout ID page. If you don't know how to find page ID just try to edit that page and get it from url /page/edit/?id=33977 ?redirect=1 probably mean that you try to to logout from homepage, which always have ID=1. 2 Link to comment Share on other sites More sharing options...
Alexander Posted March 6, 2015 Share Posted March 6, 2015 And make sure, that you checked "Should page URLs end with a slash? Yes" in your Logout template. 1 Link to comment Share on other sites More sharing options...
tuxy Posted March 6, 2015 Author Share Posted March 6, 2015 Aaaaaah, OK Alexander I added only a logout.php file with your code, but NOT a template in the backend Thanks for the useful tip, how finding the page-id Christophe Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now