<p>Try this:</p>

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

<p>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.</p>

CC BY-SA 3.0
2015-03-25T19:50:28.130Z
It's not working. So the cookie only needs to work on the homepage.
Johannes Lamers
CC BY-SA 3.0
2015-03-25T19:56:57.940Z
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
CC BY-SA 3.0
2015-03-25T20:00:35.767Z
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
CC BY-SA 3.0
2015-03-25T20:03:54.563Z
Ah, now I get it... Yes, that would be the case. Let me update my answer.
André Elvan
CC BY-SA 3.0
2015-03-25T19:46:22.267Z
2015-03-25T20:07:52.320Z
2015-03-25T20:07:52.320Z
André Elvan
<p>How can I redirect a member to account page on first visit with a cookie set? My redirect is looping now. </p>

<pre><code>{% 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 %}
</code></pre>

<p>It's not possible to visit any other URL anymore.</p>

CC BY-SA 3.0
2015-03-25T19:42:07.167Z
You want to always redirect a user if they have a particular cookie value set?
Brad Bell
CC BY-SA 3.0
2015-03-25T19:45:47.643Z
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
CC BY-SA 3.0
2015-03-25T19:47:03.807Z
It's based on this anser: http://craftcms.stackexchange.com/questions/8816/return-to-specific-locale-based-on-cookie
Johannes Lamers
<redirect><member>
How to set cookie and redirect to page one time
# How to set cookie and redirect to page one time

(Asked by: username_0 on 2015-03-25)

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.

**Tags:** redirect, member
> **Comments:**
> **username_1**: You want to always redirect a user if they have a particular cookie value set?  
> **username_0**: 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.  
> **username_0**: It's based on this anser: http://craftcms.stackexchange.com/questions/8816/return-to-specific-locale-based-on-cookie  

---

## 1 answers

### Answer by username_2 on 2015-03-25. Score: 2 | Accepted answer

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.

> **Comments:**
> **username_0**: It's not working. So the cookie only needs to work on the homepage.  
> **username_2**: 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?  
> **username_0**: 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.  
> **username_2**: Ah, now I get it... Yes, that would be the case. Let me update my answer.  

---

