Jump to content

If logout, redirect to the same page


tuxy
 Share

Recommended Posts

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

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

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);
?>
  • Like 2
Link to comment
Share on other sites

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

  • Like 2
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...