2

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).

<h1>Are you 21 or older?</h1>

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

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.

  • Try using something besides "p" for the querystring parameter. i.e. ?abc={{redirect}} and siteUrl?abc={{ craft.request.getQuery('abc'). – Brad Bell Jul 25 '17 at 18:35
  • @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 Jul 25 '17 at 21:58
  • You can't nest Twig operators. i.e. {% {{ }} %} or {{ {% %} }}. Use string concatination: {% redirect 'http://mysite.com/?age=' ~ craft.request.getQuery('age') %} – Brad Bell Jul 25 '17 at 22:36
  • @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 Jul 25 '17 at 23:40
  • craft.request.getUrlReferrer() is just checking $_SERVER['HTTP_REFERER']. Any reason why it wouldn't be set on your box? – Brad Bell Jul 26 '17 at 0:58
0

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 %}

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.