<p>Instead of relying on <code>craft.request.getUrlReferrer()</code> you could set a cookie containing the original URL before you redirect them to age verification page.</p>

<pre><code>{% if getCookie('age') %}
    Page Content
{% else %}
    {{ setCookie('originalUrl', craft.request.url ) }}
    {% redirect "age" %}
{% endif %}
</code></pre>

<p>You can than use that cookie to redirect to the original link &amp; destroy it again once you verified their age. Destroying it might not be entirely necessary as there's not duration specified so the cookie only lives for the current session. </p>

<pre><code>{{ setCookie('age', '21', now | date_modify("+365 days").timestamp ) }}
{% set redirectUrl = getCookie('originalUrl') %}
{{ setCookie( 'originalUrl' ) }}
{% redirect redirectUrl %}
</code></pre>

CC BY-SA 3.0
2017-07-26T05:47:41.760Z
2017-07-26T05:47:41.760Z
mrsmn
<p>I'm setting up a site for a brewery that requires age verification.  When a user visits the site, I check for the presence of a cookie (I'm using nystudio107's <a href="https://github.com/nystudio107/cookies" rel="nofollow noreferrer">cookie plugin</a>) and if it hasn't been set, it should redirect them to a verification page, which will then take them back to the page they were originally trying to view.</p>

<p>If I just send the user back to the homepage after verification, it works fine. But when I try to pass a variable to get them back to the page they were originally trying to view, things break. </p>

<p>Here is my code to check for the cookie and redirect to the verification page (it works as intended):</p>

<pre><code>{% if getCookie('age') %}
    Page Content
{% else %}
    {% redirect "age" %}
{% endif %}
</code></pre>

<p>Here is the verification page code (setting the redirect variable isn't working).</p>

<pre><code>&lt;h1&gt;Are you 21 or older?&lt;/h1&gt;

{% set redirect = craft.request.getUrlReferrer() %}
&lt;a href="http://google.com"&gt;No&lt;/a&gt;
&lt;a href="verified?age={{redirect}}"&gt;Yes&lt;/a&gt;
</code></pre>

<p>From here they go to the template that actually sets the cookie (again, issues with the query string):</p>

<pre><code>{{ setCookie('age', '21', now | date_modify("+365 days").timestamp ) }}
{% redirect siteUrl ~ craft.request.getQuery('age') %}
</code></pre>

<p>If I take out the redirect variable and query string, and just redirect to siteUrl, it all works fine. I'm having trouble passing the original URL along to get the user back to the page they're actually trying to go to.</p>

CC BY-SA 3.0
2017-07-25T18:35:28.300Z
Try using something besides "p" for the querystring parameter.  i.e. `?abc={{redirect}}` and `siteUrl?abc={{ craft.request.getQuery('abc')`.
Brad Bell
CC BY-SA 3.0
2017-07-25T21:58:24.817Z
@BradBell I changed it to 'age'. Now I have two issues. First, craft.request.getUrlReferrer() isn't returning any value. Second, if I set the redirect variable manually, when I load the cookie template, i get an unexpected token "operator" of value "=" ("end of statement block" expected) error on my redirect line. I updated the code in my initial question to reflect the changes I made.
artmem
CC BY-SA 3.0
2017-07-25T22:36:33.480Z
You can't nest Twig operators. i.e. `{% {{ }} %}` or `{{ {% %} }}`.  Use string concatination: `{% redirect 'http://mysite.com/?age=' ~ craft.request.getQuery('age') %}`
Brad Bell
CC BY-SA 3.0
2017-07-25T23:40:10.667Z
@BradBell Thanks. That got rid of my error. But craft.request.getUrlReferrer() in the second template still isn't returning anything. I updated the code I'm my question again to reflect the current code.
artmem
CC BY-SA 3.0
2017-07-26T00:58:11.327Z
`craft.request.getUrlReferrer()` is just checking `$_SERVER['HTTP_REFERER']`. Any reason why it wouldn't be set on your box?
Brad Bell
<redirect><request><querystring>
Set cookie and redirect back to referral URL
# Set cookie and redirect back to referral URL

(Asked by: username_0 on 2017-07-25)

I'm setting up a site for a brewery that requires age verification.  When a user visits the site, I check for the presence of a cookie (I'm using nystudio107's cookie plugin) and if it hasn't been set, it should redirect them to a verification page, which will then take them back to the page they were originally trying to view.
If I just send the user back to the homepage after verification, it works fine. But when I try to pass a variable to get them back to the page they were originally trying to view, things break. 
Here is my code to check for the cookie and redirect to the verification page (it works as intended):
{% if getCookie('age') %}
    Page Content
{% else %}
    {% redirect "age" %}
{% endif %}

Here is the verification page code (setting the redirect variable isn't working).
Are you 21 or older?

{% set redirect = craft.request.getUrlReferrer() %}
No
Yes

From here they go to the template that actually sets the cookie (again, issues with the query string):
{{ setCookie('age', '21', now | date_modify("+365 days").timestamp ) }}
{% redirect siteUrl ~ craft.request.getQuery('age') %}

If I take out the redirect variable and query string, and just redirect to siteUrl, it all works fine. I'm having trouble passing the original URL along to get the user back to the page they're actually trying to go to.

**Tags:** redirect, request, querystring
> **Comments:**
> **username_1**: Try using something besides "p" for the querystring parameter.  i.e. `?abc={{redirect}}` and `siteUrl?abc={{ craft.request.getQuery('abc')`.  
> **username_0**: @BradBell I changed it to 'age'. Now I have two issues. First, craft.request.getUrlReferrer() isn't returning any value. Second, if I set the redirect variable manually, when I load the cookie template, i get an unexpected token "operator" of value "=" ("end of statement block" expected) error on my redirect line. I updated the code in my initial question to reflect the changes I made.  
> **username_1**: You can't nest Twig operators. i.e. `{% {{ }} %}` or `{{ {% %} }}`.  Use string concatination: `{% redirect 'http://mysite.com/?age=' ~ craft.request.getQuery('age') %}`  
> **username_0**: @BradBell Thanks. That got rid of my error. But craft.request.getUrlReferrer() in the second template still isn't returning anything. I updated the code I'm my question again to reflect the current code.  
> **username_1**: `craft.request.getUrlReferrer()` is just checking `$_SERVER['HTTP_REFERER']`. Any reason why it wouldn't be set on your box?  

---

## 1 answers

### Answer by username_2 on 2017-07-26. Score: 0 | Accepted answer

Instead of relying on craft.request.getUrlReferrer() you could set a cookie containing the original URL before you redirect them to age verification page.
{% if getCookie('age') %}
    Page Content
{% else %}
    {{ setCookie('originalUrl', craft.request.url ) }}
    {% redirect "age" %}
{% endif %}

You can than use that cookie to redirect to the original link & destroy it again once you verified their age. Destroying it might not be entirely necessary as there's not duration specified so the cookie only lives for the current session. 
{{ setCookie('age', '21', now | date_modify("+365 days").timestamp ) }}
{% set redirectUrl = getCookie('originalUrl') %}
{{ setCookie( 'originalUrl' ) }}
{% redirect redirectUrl %}


---

