1

How can I redirect a member to account page on first visit with a cookie set? My redirect is looping now.

{% if currentUser %}
   {% set name = currentUser.firstName %}
   {% do craft.lj_cookies.set('account', name) %}



   {% set redirect = craft.lj_cookies.get('account') %}

   {% if redirect == name %}
       {% redirect name %}
   {% endif %}

{% endif %}

It's not possible to visit any other URL anymore.

  • You want to always redirect a user if they have a particular cookie value set? – Brad Bell Mar 25 '15 at 19:42
  • The redirect to the account is just an example. In my case i'm working on a kind of webshop. My members need to fill in there gender. If they enter the site I want them to redirect to the shop with male (shop/male of shop/female) product. After the redirect is up to the member where to go next. Could me male products etc. So just 1 time redirect. – Johannes Lamers Mar 25 '15 at 19:45
  • It's based on this anser: craftcms.stackexchange.com/questions/8816/… – Johannes Lamers Mar 25 '15 at 19:47
2

Try this:

{% if currentUser %}
    {% set name = currentUser.firstName %}
    {% if craft.lj_cookies.get('account') != name %}
        {% do craft.lj_cookies.set('account', name) %}
        {% redirect name %}
    {% endif %}
{% endif %}

In you're original code you're setting the cookie before getting it into the redirect variable, so the {% if redirect == name %} conditional is always true.

  • It's not working. So the cookie only needs to work on the homepage. – Johannes Lamers Mar 25 '15 at 19:50
  • So, this code is in your homepage template (not your layout)? And you want to redirect to /male or /female (as per your comment above) depending on the users gender? Can you access these url's directly when a) you're logged in or b) you're not logged in? – André Elvan Mar 25 '15 at 19:56
  • Yes its on the homepage. So after first visit the redirect gets me to the right page. But if I want to go back to the homepage there is the redirect again. So just one time a redirect. After that its up to the member. – Johannes Lamers Mar 25 '15 at 20:00
  • Ah, now I get it... Yes, that would be the case. Let me update my answer. – André Elvan Mar 25 '15 at 20:03

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.