9

I am new to working with Expression Engine, so this might not be possible. The site I am working on has some pages that are private to members only. I'm assuming this is a built in feature of Expression Engine and not some type of add-on. When a member who is not logged in reaches a page which requires login, I would like them to be redirected back to the page they were on after they log in. Currently it is redirecting them to a specific member page (and I'm not sure where that is configured).

Update:

After further investigation, there are two "member" modules installed: Freemember and Member. There is a link to a custom login page and in the html there is a {login_form} tag. I can't find where or who defines this variable so I don't know what to do next.

Update:

I found the source of the {login_form} tag. This was being defined in a template using a plugin called CE variables. Here's a sample of the template:

{exp:ce_vars:set_content name="login_form"}
{exp:freemember:login form_id="login" return="/member-resources" error_handling="inline" error_delimiters='<small>|</small>'}
    ...some html...
{/exp:freemember:login}
{/exp:ce_vars:set_content}

Obviously, I found the source of the redirect (the return attribute). So now it is a matter of passing the "return to URL" in the link to the login form.

Solution:

Freemember provides a PREVIOUS_URL constant that you can use instead:

{exp:freemember:login return="PREVIOUS_URL"}
5

Here's how I'm doing this using Profile:Edit, which is an add-on. I assume Zoo Visitor will support something similar.

If you visit one of my news article pages, because it was linked in an email, at the top of that template I have:

{if logged_out}{redirect='/members/login/{segment_1}/{segment_2}'}{/if}

You could add all your segments there. The point is, I'm dynamically passing the current URL to my member login form.

Within that login form template, I have my Profile:Edit tag:

{exp:profile:login return="/{if segment_3!=''}{segment_3}{/if}{if segment_4!=''}/{segment_4}{/if}{if segment_5!=''}/{segment_5}{/if}" class="nice"}

That will look at up to 5 segments to match the URL passed in, and redirect back to it.

You didn't say how you plan on protecting the pages that require login or if you did that already. I assume you're using a tag at the top of those templates to check logged-in/logged-out status and redirecting if the user is not logged in.

  • 1
    Great answer. I've been using URL Helper which is combination of popular extensions/plugins. The one variable that comes in handy is {all_segments}, which could be used in your return parameter instead of all the conditionals. ie. return="{all_segments}" – Siebird Feb 6 '13 at 17:38
  • That's great to know. Bookmarking that now just for the {all_segments} tag. – Mark J. Reeves Feb 6 '13 at 17:40
  • Thanks for your answer. I'm not sure if it will work for me though. After further investigation, there are two "member" modules installed: Freemember and Member. There is a link to a custom login page and in the html there is a {login_form} tag. I can't find where or who defines this variable so I don't know what to do next. – Andrew Feb 6 '13 at 18:10
  • 1
    Freemember has a return param on its login tag. You can use that the same way I used the return param to set the redirect URL: github.com/expressodev/freemember/wiki/Login-Tag – Mark J. Reeves Feb 6 '13 at 18:12
8

I know this is marked answered for 8 months, but here is an answer without any add-ons needed, which is in the original question.

  1. Set template access for guests to off. Have it load a login template instead.

  2. In the login template do this for the login form:

    {exp:member:login_form return="{segment_1}/{segment_2}/{segment_3}"}
    

What I like about this solution is that you can send a link to someone through email for example. They get the URL, aren't really redirected, it's just a different template that shows up. When they submit the login form the page reloads with the right template. They never leave the URL they should be on.

  • Great solution! This totally saved me from having to add messy conditionals to all my templates and avoid unnecessary server overhead. Also, in case anyone is wondering, this does also work with freemember as {segment} tags are global conditionals. – positlux May 3 '14 at 4:27
0

Freemember provides a built in PREVIOUS_URL constant that you can use to achieve this:

{exp:freemember:login return="PREVIOUS_URL"}

Note that this redirects the user to the page they were visiting before your login page. So say your customer is browsing /blog/first-post, then they click a login link and are taken to your /account/login page. This constant will redirect the user back to /blog/first-post.

If on the other hand, you have a login form in your header (or a popup lightbox), and want to redirect the user to the current page after login, you can simply omit the return="" parameter, as that behavior is the default:

{exp:freemember:login}
0

Without add-ons you can do this.

if logged_in /blog/first-post /if

if logged_out /account/login/blog/first-post /if

Then use segments 3 and 4 for the return parameter of exp:member:login_form

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.