Jump to content

Page redirection with LoginRegister


Recommended Posts

Hello everyone, 

I'm rather new in developing with ProcessWire and loving it every day more.

I found that new module "LoginRegister", which is amazing. However, I would be happy about one feature. I wasn't able to solve this myself. 

Is there a possibility to achieve, that a user is redirected to a site (which is denied for guest users) he actually wants to view?
Let's say I have a restricted sub-page called "test", which redirects to LoginRegister login form if a user is not logged-in.
Right now, the redirection after successful log in is static and points to a pre defined site (let's say: "welcome").
So when a user tries to call mypage.com/test and logs in successful he gets to mypage.com/welcome instead of mypage.com/test.. 

Hope you have some hints for me on how to achieve this. 

Link to comment
Share on other sites

Hi @wbmnfktr,

thanks for your reply. 

That code works fine, but only with a condition, which is problem for my current setup. 

Pre-condition that psy's code works is that "_init.php" gets loaded at all. As far as I see, that file only get's loaded when user role "guest" has access to the corresponding template, but not if restricted permission (and so redirection) is set in ProcessWire backend. 
However, only certain user roles should have access to the template. 

As a workaround I could add permission handling in the specific template, but then I would loose control in ProcessWire backend as I would have to hardcode user roles. 

Maybe I'm wrong with this or do you know how to set "returnPage" variable before ProcessWire redirection?  

Link to comment
Share on other sites

28 minutes ago, androbey said:

Maybe I'm wrong with this or do you know how to set "returnPage" variable before ProcessWire redirection?

When you deny view access to the guest role you can enter a URL to redirect to. In this field you can use {id} in a GET variable to track which page the user was trying to access.


  • Like 3
Link to comment
Share on other sites

_init,php isn't a regular template file.
It's more kind of a universal settings file with variables and presets you might need later on your page(s).

So you could add the following line to site/config.php

$config->prependTemplateFile = '_init.php';

The _init.php in /site/templates/ will be included every page load and therefore the login check is always in place.

Within this file you could and should add psy's / your code


$loginPage = $pages->get('template=loginregister'); // in my case yours might be different

if(!$user->isLoggedin() && $page->id!=$loginPage->id) { // checks if user is logged in and not on login page
  $session->set('returnPage', '/path/to/welcome/'); // set your welcome site

This would redirect every user that is not logged in to your login page and sets the returnPage.

Be careful with the check conditions. You might want to fine tune this check.

  • Like 2
Link to comment
Share on other sites

Thank you both, Robin S and wbmnfktr!

I managed to achieve the desired behaviour by setting a GET Variable in the redirect URL and adjusted the code. 

I already did prepend the "_init.php" file, but was probably overwriting the session variable.. 

Here's the code I use in my "_init.php": 

if(!$user->isLoggedin()) { 
//'returnID' is my GET variable and page id 1055 is my start page (not log-in page).
  if(isset($_GET['returnID']) && $page->id!=1055 ){
    $session->set('returnPage', $_GET['returnID']);

Thanks again, you helped me a lot. 

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

  • Create New...