Jump to content
adrianmak

redirect to last/previous page after user log-in

Recommended Posts

Some page required certain role to access, and will redirect to a user log-in page

I found this post, it handle the redirection manually, which could allow to store page id to session variable or redirect url with paramater.

https://processwire.com/talk/topic/6387-redirect-to-previous-page/

However, I config the page redirect in template access tab, which I put log-in page id where a page redirection.

Any clue to store a page with this method ?

Share this post


Link to post
Share on other sites

You could use javascript to redirect using the code below on your login page:

<?php 

if($user->isLoggedin()) {
   echo "<script>history.go(-1);</script>";
}

?>

Share this post


Link to post
Share on other sites

You could use javascript to redirect using the code below on your login page:

<?php 

if($user->isLoggedin()) {
   echo "<script>history.go(-1);</script>";
}

?>

I'm sorry. I could not follow

This is my full code of login.php

<?php
$content = "";

if ($input->post->user && $input->post->pass) {
    $username = $sanitizer->username($input->post->user);
    $password = $input->post->pass;

    try {
        if ($session->login($username, $password)) {
            $session->redirect($pages->get("template=home")->url);  // redirect to home after user login
        } else {
            $error = "Wrong username or password. Login failed.";
        }
    } catch (Exception $e) {
        $error = $e->getMessage();
    }
}

$content .= "
        <div class='login-box'>
        <div class='panel panel-default'>
            <div class='panel-heading'>
                <h3 class='panel-title'><span class='lock-icon'></span><strong>Member Log-in</strong></h3>
            </div>
            <div class='panel-body'>
                <form role='form' action='./' method='post'>
                    <div class='message-error'>$error</div>
                    <div class='form-group'>
                        <label for='user'>Username</label>
                        <input type='text' name='user' id='user' class='form-control' placeholder='Username' />
                    </div>
                    <div class='form-group'>
                        <label for='pass'>Password</label>
                        <input type='password' name='pass' id='pass' class='form-control' placeholder='Password' />
                    </div>
                    <button type='submit' class='btn btn-sm btn-primary'>Login</button>
                </form>
            </div>
        </div>
        </div>
";

Should I replace the code

            $session->redirect($pages->get("template=home")->url);  // redirect to home after user login

With

echo "<script>history.go(-1);</script>";

?

Share this post


Link to post
Share on other sites

Correct :-)

it didn't work.

After logged in, the page stay at login form, instead of go back to last page

Share this post


Link to post
Share on other sites

Looks like you're using delayed output method. Therefore JS will be outputted in the wrong place. Try:

$content .= "<script>history.go(-1);</script>";

Share this post


Link to post
Share on other sites

Looks like you're using delayed output method. Therefore JS will be outputted in the wrong place. Try:

$content .= "<script>history.go(-1);</script>";

it's still not working. The same as using echo.

After logged in, the page stay at login form, instead of go back to last page

Share this post


Link to post
Share on other sites

I'm not a big fan of the javascript solution. Good in theory but lets say you have a form error and your return to the same form then submit successfully, I get a session expired error (because its returning to the form url once more, not the previous page). 

 

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...