I would like someone to help with how to redirect a returning user who has a cookie set in his/her browser to http://google.co.uk. I'm working on a Age Verification System in which everything is working as it should in wordpress but the only issue i've been trying to figure out is how to redirect users who are underage to google.co.uk until the cookie expires.
This is the process, when a users visits the website, they are prompted to fill in their D.O.B, if within the allowed age (18+), they are welcomed to the website and can continue browsing the website but if underaged, i used Ajax and PHP (PHP to sets the cookies name VAage which has a content of under 18 as seen in the screenshot below) while ajax handles the user's experience/ interaction with the form without having to reload page.
Now the part that hasn't been cooperating is that... when a user is under 18 and has been redirected to http://google.co.uk (which to this point is working fine) and decides to revisit the website immediately or revisit the website while the cookies hasn't expired or being deleted from his/her browser should be redirected back to google.co.uk again and again (preventing them from access the website).
I have tried adding this code to function.php file, header.php, index.php of the theme but doesn't work;
function is_user_with_VAage_cookies () {
if ( isset( $_COOKIE['VAage'] ) ) {
header('Location: https://google.co.uk');
exit;
}
}
i as well tried using JavaScript instead of using PHP's header() function, like this,
function is_user_with_VAage_cookies () {
if ( isset( $_COOKIE['VAage'] ) ) {
?>
<script type="text/javascript">var win = window.open('https://google.co.uk', '_self');</script>
<?php
}
}
i'm not sure what i'm doing wrong or if there's a hook to make this work. I have tried hooking it like this add_action('init', 'is_user_with_VAage_cookies');
(as seen here) but each time i test the Age verification and try coming back to the website after being redirected away for being under 18, it still loads up the website which i don't want. i want underaged users to be redirected to google immediately they try accessing the website again till the cookie expires (which is set to expire 7days).
Thanks in advance for your help